<?php
namespace app\index\controller;

use app\index\model\AccountDiaoModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\ArchiveDiaoModel;
use app\index\model\ArchiveJiaoListModel;
use app\index\model\ArchiveJiaoModel;
use app\index\model\ArchiveModel;
use app\index\model\ArchiveOutModel;
use app\index\model\ArchiveZhuanModel;
use app\index\model\BidBookModel;
use app\index\model\BidFeiModel;
use app\index\model\BidGrantBookModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertDownListModel;
use app\index\model\CertDownModel;
use app\index\model\CertModel;
use app\index\model\CertPerruModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidListModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\CertZhaoModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiModel;
use app\index\model\DangJieListModel;
use app\index\model\DangJieModel;
use app\index\model\DangModel;
use app\index\model\DealcomGengModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieListModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceJinModel;
use app\index\model\DeviceLinListModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanListModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginJunjieListModel;
use app\index\model\EnginJunjieModel;
use app\index\model\EnginJunyanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginSpeedModel;
use app\index\model\EnginStampListModel;
use app\index\model\EnginStampModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\FileModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowSetModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\KaoBanModel;
use app\index\model\KaochaiModel;
use app\index\model\KaoChuModel;
use app\index\model\KaoJiaModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourJieListModel;
use app\index\model\LabourJieModel;
use app\index\model\LabourLingListModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenListModel;
use app\index\model\LabourShenModel;
use app\index\model\LabourYongListModel;
use app\index\model\LabourYongModel;
use app\index\model\PaymentBaoListModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\SealGaiListModel;
use app\index\model\SealGaiModel;
use app\index\model\SealModel;
use app\index\model\SpeedMonthModel;
use app\index\model\StuffCaiListModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffJieListModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\SubbaoJieListModel;
use app\index\model\SubbaoJieModel;
use app\index\model\SubbaoShenListModel;
use app\index\model\SubbaoShenModel;
use app\index\model\TongyongModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\VisaModel;
use app\index\model\WageListModel;
use app\index\model\WageModel;
use app\index\model\WenBanModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuLingModel;
use app\index\model\WuRuListModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\DeviceService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\HumanService;
use app\index\service\IncomeService;
use app\index\service\LabourService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\SceneService;
use app\index\service\StuffService;
use app\index\service\SubbaoService;
use app\index\service\UserService;
use app\index\service\XingService;
use app\index\validate\CertUse;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\Request;

class Flowdata extends Common
{
    //##############################################  投标  ##############################################
    //##############################################  投标  ##############################################

    public function add_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new BidService())->check_bid_name($param['name'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }
                if(empty($param['enddt_tou'])){
                    return outputJson(4000, '请选择投标截止时间');
                }
                if(empty($param['touer'])){
                    return outputJson(4000, '请选择投标负责人');
                }
                if(empty($param['touunit'])){
                    return outputJson(4000, '请选择投标单位');
                }
                if(empty($param['business'])){
                    return outputJson(4000, '请选择项目属性');
                }
                if($param['business'] == 2){
                    if(empty($param['lianunit'])){
                        return outputJson(4000, '请选择联营单位');
                    }
                    if(empty($param['isfei'])){
                        return outputJson(4000, '请选择是否收费');
                    }
                }
            }

            $list = [];
            if($param['business'] == 2){
                if($param['isfei'] == '是'){
                    $list = array_filter($param['list']);
                    $param['yingshou'] = 0;
                    $param['yishou'] = 0;
                    foreach ($list as $vvv){
                        $param['yingshou'] += $vvv['yingshou'];
                        $param['yishou'] += $vvv['yishou'];
                    }
                    $param['daishou'] = round($param['yingshou'] - $param['yishou'], 2);
                }else{
                    $param['yingshou'] = 0;
                    $param['yishou'] = 0;
                    $param['daishou'] = 0;
                }
            }else{
                $param['lianunit'] = '';
                $param['isfei'] = '';
                $param['feishuo'] = '';
                $param['yingshou'] = 0;
                $param['yishou'] = 0;
                $param['daishou'] = 0;
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            unset($param['list']);


            foreach ($param as $k=>$v){
                if(strpos($k, 'feixiang') !== false){
                    unset($param[$k]);
                }
            }


            Db::startTrans();
            try {
                $id = BidModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['bidid'] = $id;
                    BidFeiModel::insert($vv);
                }

                (new FlowService())->save_bill('bid', $id);

                (new OptService())->relation_file($files,'bid',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid', $id, '投标项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $poolid = $request->get('poolid',0,'int');
            $poolinfo = BidPoolModel::where(['id'=>$poolid])->find();
            $data['files'] = [];
            if($poolinfo){
                $poolinfo['chanid_info'] = (new UserService())->get_users_part_arr($poolinfo['chanid'], 'dept');

                $pool_file = FileModel::where(['table'=>'bid_pool','mid'=>$poolinfo['id']])->select()->toArray();
                $copyIdArr = [];
                if($pool_file){
                    foreach ($pool_file as $k=>$v){
                        if(file_exists($v['oripath'])){
                            $new_oripath = '../infoasset/'.date('Ymd').'/';
                            if(!file_exists($new_oripath)){
                                mkdir($new_oripath,'0777',true);
                            }
                            $new_oripath = $new_oripath.'copy'.substr($v['oripath'], 22);
                            if(copy($v['oripath'],'./'.$new_oripath)){
                                unset($v['id']);
                                $v['table'] = 'bid';
                                $v['oripath'] = $new_oripath;
                                $v['created_user'] = $this->userinfo['id'];
                                $v['created_at'] = date('Y-m-d H:i:s');
                                $ins_id = FileModel::insertGetId($v);
                                $copyIdArr[] = $ins_id;
                                $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                                FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]);
                            }
                        }
                    }
                }

                $data['files'] = FileModel::field('id,table,mid,name,ext,size,encpath,fileplain,created_user,created_at')
                    ->where('id','in',$copyIdArr)->select()->toArray();
                foreach ($data['files'] as &$item){
                    $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                }
            }

            $data['poolinfo'] = $poolinfo;

            $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['feixiang'] = (new OptService())->get_base_info('bid_feixiang');


            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bid');
            $data['isadvan'] = (new FlowService())->check_isadvan('bid');
            $data['isnext'] = (new FlowService())->check_isnext('bid');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new BidService())->check_bid_name($param['name'], $param['id'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }
                if(empty($param['enddt_tou'])){
                    return outputJson(4000, '请选择投标截止时间');
                }
                if(empty($param['touer'])){
                    return outputJson(4000, '请选择投标负责人');
                }
                if(empty($param['touunit'])){
                    return outputJson(4000, '请选择投标单位');
                }
                if(empty($param['business'])){
                    return outputJson(4000, '请选择项目属性');
                }
                if($param['business'] == 2){
                    if(empty($param['lianunit'])){
                        return outputJson(4000, '请选择联营单位');
                    }
                    if(empty($param['isfei'])){
                        return outputJson(4000, '请选择是否收费');
                    }
                }
            }

            $list = [];
            if($param['business'] == 2){
                if($param['isfei'] == '是'){
                    $list = array_filter($param['list']);
                    $param['yingshou'] = 0;
                    $param['yishou'] = 0;
                    foreach ($list as $vvv){
                        $param['yingshou'] += $vvv['yingshou'];
                        $param['yishou'] += $vvv['yishou'];
                    }
                    $param['daishou'] = round($param['yingshou'] - $param['yishou'], 2);
                }else{
                    $param['yingshou'] = 0;
                    $param['yishou'] = 0;
                    $param['daishou'] = 0;
                }
            }else{
                $param['lianunit'] = '';
                $param['isfei'] = '';
                $param['feishuo'] = '';
                $param['yingshou'] = 0;
                $param['yishou'] = 0;
                $param['daishou'] = 0;
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);
            unset($param['list']);


            foreach ($param as $k=>$v){
                if(strpos($k, 'feixiang') !== false){
                    unset($param[$k]);
                }
            }

            Db::startTrans();
            try {
                BidModel::update($param);

                BidFeiModel::where(['bidid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['bidid'] = $param['id'];
                    BidFeiModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bid', $param['id']);

                }else{
                    (new FlowService())->save_bill('bid', $param['id']);
                }

                (new OptService())->relation_file($files,'bid',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid', $param['id'], '投标项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['data']['touunit_info'] = (new UserService())->get_users_part_arr($data['data']['touunit'], 'dept');
            $data['data']['touer_info'] = (new UserService())->get_users_part_arr($data['data']['touer'], 'user');


            $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['feixiang'] = (new OptService())->get_base_info('bid_feixiang');



            $feilist = BidFeiModel::where(['bidid'=>$data['data']['id']])->select()->toArray();
            foreach ($feilist as &$item){
                $item['feixiang_name'] = (new OptService())->get_base_showname('bid_feixiang', $item['feixiang']);
            }
            $data['feilist'] = $feilist;


            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bid');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bid',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bid');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bid_book(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入标书名称');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $result = BidBookModel::create($param);
                (new FlowService())->save_bill('bid_book', $result->id);
                (new OptService())->relation_file($files,'bid_book',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_book', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_book', $result->id, '标书评审');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$result->id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bid_book');
            $data['isadvan'] = (new FlowService())->check_isadvan('bid_book');
            $data['isnext'] = (new FlowService())->check_isnext('bid_book');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['flowdt'] = date('Y-m-d H:i:s');
            $data['maker_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid_book(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入标书名称');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            Db::startTrans();
            try {
                BidBookModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bid_book', $param['id']);
                }else{
                    (new FlowService())->save_bill('bid_book', $param['id']);
                }
                (new OptService())->relation_file($files,'bid_book',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_book', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_book', $param['id'], '标书评审');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                halt($e);
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidBookModel::where(['id'=>$id])->find();
            $bid = BidModel::where('id',$result['bidid'])->find();
            $result['bid_name'] = $bid['name'];

            $result['business'] = $bid['business'] == 1 ? '自营' : '联营';
            $result['enddt_tou'] = $bid['enddt_tou'];
            $result['maker_info'] = (new UserService())->get_users_part_arr($result['maker'], 'user');
            $data['data'] = $result;

            $data['files'] = (new OptService())->get_file('bid_book', $result['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($result['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($result['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($result['nexter'], 'user');
            $data['flowsetname'] = FlowSetModel::get_name('bid_book');
            $data['isadvan'] = (new FlowService())->check_isadvan_edit('bid_book',$result);
            $data['isnext'] = (new FlowService())->check_isnext('bid_book');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bid_grant_book(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入授权委托人');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $param['name'] = implode('[]',$param['name']);
            $param['number'] = implode('[]',$param['number']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $result = BidGrantBookModel::create($param);
                (new FlowService())->save_bill('bid_grant_book', $result->id);
                (new OptService())->relation_file($files,'bid_grant_book',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_grant_book', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_grant_book', $result->id, '授权委托书');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$result->id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bid_grant_book');
            $data['isadvan'] = (new FlowService())->check_isadvan('bid_grant_book');
            $data['isnext'] = (new FlowService())->check_isnext('bid_grant_book');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['flowdt'] = date('Y-m-d H:i:s');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid_grant_book(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入标书名称');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $param['name'] = implode('[]',$param['name']);
            $param['number'] = implode('[]',$param['number']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            Db::startTrans();
            try {
                BidGrantBookModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bid_grant_book', $param['id']);
                }else{
                    (new FlowService())->save_bill('bid_grant_book', $param['id']);
                }
                (new OptService())->relation_file($files,'bid_grant_book',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_grant_book', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_grant_book', $param['id'], '授权委托书');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                halt($e);
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidGrantBookModel::where(['id'=>$id])->find();
            $bid = BidModel::where('id',$result['bidid'])->find();
            $result['bid_name'] = $bid['name'];
            $result['bid_number'] = $bid['number'];
            $result['name'] = explode('[]',$result['name']);
            $result['number'] = explode('[]',$result['number']);
            $data['data'] = $result;

            $data['files'] = (new OptService())->get_file('bid_grant_book', $result['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($result['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($result['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($result['nexter'], 'user');
            $data['flowsetname'] = FlowSetModel::get_name('bid_grant_book');
            $data['isadvan'] = (new FlowService())->check_isadvan_edit('bid_grant_book',$result);
            $data['isnext'] = (new FlowService())->check_isnext('bid_grant_book');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    //##############################################  财务  ##############################################
    //##############################################  财务  ##############################################
    public function add_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = DealcomModel::insertGetId($param);
                (new FlowService())->save_bill('dealcom', $id);
                (new OptService())->relation_file($files,'dealcom',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $id, '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealcom');
            $data['isnext'] = (new FlowService())->check_isnext('dealcom');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }

            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                DealcomModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealcom', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealcom', $param['id']);
                }

                (new OptService())->relation_file($files,'dealcom',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $param['id'], '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealcom',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealcom');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dealcom_geng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dealid'])){
                    return outputJson(4000, '请选择变更往来单位');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['dealid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $copyFiles = [];
            foreach ($files as $vvv){
                $arr = FileModel::where(['id'=>$vvv['id']])->find()->toArray();
                unset($arr['id']);
                $copyId = FileModel::insertGetId($arr);
                $copyFiles[] = ['id'=>$copyId];
            }
            $files = $copyFiles;


            Db::startTrans();
            try {
                $id = DealcomGengModel::insertGetId($param);
                (new FlowService())->save_bill('dealcom_geng', $id);
                (new OptService())->relation_file($files,'dealcom_geng',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom_geng', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom_geng', $id, '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('dealcom_geng');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealcom_geng');
            $data['isnext'] = (new FlowService())->check_isnext('dealcom_geng');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom_geng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dealid'])){
                    return outputJson(4000, '请选择变更往来单位');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['dealid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                DealcomGengModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealcom_geng', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealcom_geng', $param['id']);
                }

                (new OptService())->relation_file($files,'dealcom_geng',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom_geng', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom_geng', $param['id'], '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomGengModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $dealid_info = DealcomModel::where(['id'=>$data['data']['dealid']])->find();
            $data['dealid_info'] = $dealid_info;

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['files'] = (new OptService())->get_file('dealcom_geng', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealcom_geng');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealcom_geng',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealcom_geng');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if($param['bondtype'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }else{
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }

                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }



            $param['number'] = (new PaymentService())->create_bond_jiao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $copyFiles = [];
            foreach ($files as $vvv){
                $arr = FileModel::where(['id'=>$vvv['id']])->find()->toArray();
                unset($arr['id']);
                $copyId = FileModel::insertGetId($arr);
                $copyFiles[] = ['id'=>$copyId];
            }
            $files = $copyFiles;

            Db::startTrans();
            try {
                $id = EnginBondJiaoModel::insertGetId($param);

                (new FlowService())->save_bill('engin_bond_jiao', $id);

                (new OptService())->relation_file($files,'engin_bond_jiao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_jiao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_jiao', $id, '项目保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_bond_jiao_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_bond_jiao');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_bond_jiao');
            $data['isnext'] = (new FlowService())->check_isnext('engin_bond_jiao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if($param['bondtype'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }else{
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }

                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondJiaoModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_bond_jiao', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_bond_jiao', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_bond_jiao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_jiao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_jiao', $param['id'], '项目保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('engin_bond_jiao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_bond_jiao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_bond_jiao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_bond_jiao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shouid'])){
                    return outputJson(4000, '请选择关联收取');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }




            $param['number'] = (new PaymentService())->create_bond_fan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginBondFanModel::insertGetId($param);

                (new FlowService())->save_bill('engin_bond_fan', $id);

                (new OptService())->relation_file($files,'engin_bond_fan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_fan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_fan', $id, '项目保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_bond_fan_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_bond_fan');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_bond_fan');
            $data['isnext'] = (new FlowService())->check_isnext('engin_bond_fan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shouid'])){
                    return outputJson(4000, '请选择关联收取');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondFanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_bond_fan', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_bond_fan', $param['id']);
                }


                (new OptService())->relation_file($files,'engin_bond_fan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_fan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_fan', $param['id'], '项目保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondFanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shouinfo = EnginBondShouModel::get_info_by_pk($data['data']['shouid']);
            if($shouinfo['bondtype'] == 1){
                $shouinfo['enginname'] = BidModel::where(['id'=>$shouinfo['bidid']])->value('name');
            }else{
                $shouinfo['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            }

            $shouinfo['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
            $data['shouinfo'] = $shouinfo;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('engin_bond_fan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_bond_fan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_bond_fan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_bond_fan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if($param['bondtype'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }else{
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }
                if(empty($param['hantype'])){
                    return outputJson(4000, '请选择保函类别');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入保函金额');
                }
            }





            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginBondKaiModel::insertGetId($param);

                (new FlowService())->save_bill('engin_bond_kai', $id);

                (new OptService())->relation_file($files,'engin_bond_kai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_kai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_kai', $id, '项目保函开具');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_bond_kai');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_bond_kai');
            $data['isnext'] = (new FlowService())->check_isnext('engin_bond_kai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if($param['bondtype'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }else{
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                }
                if(empty($param['hantype'])){
                    return outputJson(4000, '请选择保函类别');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入保函金额');
                }
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondKaiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_bond_kai', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_bond_kai', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_bond_kai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_kai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_kai', $param['id'], '项目保函开具');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondKaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
            $data['files'] = (new OptService())->get_file('engin_bond_kai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_bond_kai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_bond_kai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_bond_kai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_neifu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();
                if(!$exist){
                    return outputJson(4000, '该项目暂无施工合同,无法提交');
                }

                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }


            $param['number'] = (new PaymentService())->create_payment_neifu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = PaymentNeifuModel::insertGetId($param);

                (new FlowService())->save_bill('payment_neifu', $id);

                (new OptService())->relation_file($files,'payment_neifu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_neifu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_neifu', $id, '内包付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_neifu_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_neifu');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_neifu');
            $data['isnext'] = (new FlowService())->check_isnext('payment_neifu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_neifu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();
                if(!$exist){
                    return outputJson(4000, '该项目暂无施工合同,无法提交');
                }

                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                PaymentNeifuModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_neifu', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_neifu', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_neifu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_neifu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_neifu', $param['id'], '内包付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentNeifuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            if($engininfo){
                $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
                if($neiid){
                    $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                    $engininfo['nei_name'] = $neiInfo['name'];
                    $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
                }
                $engininfo['incomefee'] = (new EnginService())->get_engin_incomeengin($engininfo['id']);
                $engininfo['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
                $data['engininfo'] = $engininfo;
            }


            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_neifu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_neifu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_neifu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_neifu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['yiju'])){
                    return outputJson(4000, '请选择付款依据');
                }else{
                    if($param['yiju'] != 'none'){
                        if(empty($param['yijuid'])){
                            return outputJson(4000, '请选择依据(名称)');
                        }
                    }
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择付款对象');
                }
            }

            $param['caiid'] = 0;
            if($param['yiju'] == 'concaistuff'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'stuffcai'){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffru'){
                $param['caiid'] = StuffRuModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffjie'){
                $param['caiid'] = StuffJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'concailabour'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'labourshen'){
                $param['caiid'] = LabourShenModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labourjie'){
                $param['caiid'] = LabourJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labouryong'){
                $param['caiid'] = LabourYongModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labourling'){
                $param['caiid'] = 0;
            }elseif($param['yiju'] == 'concaidevice'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'devicejie'){
                $param['caiid'] = DeviceJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'devicelin'){
                $param['caiid'] = 0;
            }elseif($param['yiju'] == 'concaisubbao'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'subbaoshen'){
                $param['caiid'] = SubbaoShenModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'subbaojie'){
                $param['caiid'] = SubbaoJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'concaiqi'){
                $param['caiid'] = $param['yijuid'];
            }

            if($param['isturn'] > 0){
                $qingfee = (new PaymentService())->get_yiju_qingfee($param['yijuid'], $param['yiju']);
                if($param['yiju'] == 'stuffcai'){
                    $danfee = StuffCaiModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'stuffru'){
                    $danfee = StuffRuModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'stuffjie'){
                    $danfee = StuffJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourshen'){
                    $danfee = LabourShenModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourjie'){
                    $danfee = LabourJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labouryong'){
                    $danfee = LabourYongModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourling'){
                    $danfee = LabourLingModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'devicejie'){
                    $danfee = DeviceJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'devicelin'){
                    $danfee = DeviceLinModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'subbaoshen'){
                    $danfee = SubbaoShenModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'subbaojie'){
                    $danfee = SubbaoJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }
            }

            $param['number'] = (new PaymentService())->create_payment_engin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = PaymentEnginModel::insertGetId($param);

                (new FlowService())->save_bill('payment_engin', $id);

                (new OptService())->relation_file($files,'payment_engin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_engin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_engin', $id, '项目付款(供应商)');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['budid'] = (new PaymentService())->get_payment_budid();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_engin_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_engin');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_engin');
            $data['isnext'] = (new FlowService())->check_isnext('payment_engin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['yiju'])){
                    return outputJson(4000, '请选择付款依据');
                }else{
                    if($param['yiju'] != 'none'){
                        if(empty($param['yijuid'])){
                            return outputJson(4000, '请选择依据(名称)');
                        }
                    }
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择付款对象');
                }
            }

            $param['caiid'] = 0;
            if($param['yiju'] == 'concaistuff'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'stuffcai'){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffru'){
                $param['caiid'] = StuffRuModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffjie'){
                $param['caiid'] = StuffJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'concailabour'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'labourshen'){
                $param['caiid'] = LabourShenModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labourjie'){
                $param['caiid'] = LabourJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labouryong'){
                $param['caiid'] = LabourYongModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'labourling'){
                $param['caiid'] = 0;
            }elseif($param['yiju'] == 'concaidevice'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'devicejie'){
                $param['caiid'] = DeviceJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'devicelin'){
                $param['caiid'] = 0;
            }elseif($param['yiju'] == 'concaisubbao'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'subbaoshen'){
                $param['caiid'] = SubbaoShenModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'subbaojie'){
                $param['caiid'] = SubbaoJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'concaiqi'){
                $param['caiid'] = $param['yijuid'];
            }


            if($param['isturn'] > 0){
                $qingfee = (new PaymentService())->get_yiju_qingfee($param['yijuid'], $param['yiju'], $param['id']);
                if($param['yiju'] == 'stuffcai'){
                    $danfee = StuffCaiModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'stuffru'){
                    $danfee = StuffRuModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'stuffjie'){
                    $danfee = StuffJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourshen'){
                    $danfee = LabourShenModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourjie'){
                    $danfee = LabourJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labouryong'){
                    $danfee = LabourYongModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'labourling'){
                    $danfee = LabourLingModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'devicejie'){
                    $danfee = DeviceJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'devicelin'){
                    $danfee = DeviceLinModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'subbaoshen'){
                    $danfee = SubbaoShenModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }elseif($param['yiju'] == 'subbaojie'){
                    $danfee = SubbaoJieModel::where(['id'=>$param['yijuid']])->value('money');
                    if( ($qingfee+$param['money']) > $danfee ){
                        return outputJson(4000, '所选付款依据的累计请款金额（含本次）大于单据金额，请重新处理。');
                    }
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                PaymentEnginModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_engin', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_engin', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_engin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_engin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_engin', $param['id'], '项目付款(供应商)');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $yijuidInfo = [];
            if($data['data']['yiju'] == 'concaistuff'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料合同';
            }elseif($data['data']['yiju'] == 'stuffcai'){
                $yijuidInfo = StuffCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料采购单';
            }elseif($data['data']['yiju'] == 'stuffru'){
                $yijuidInfo = StuffRuModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料入库单';
            }elseif($data['data']['yiju'] == 'stuffjie'){
                $yijuidInfo = StuffJieModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料结算单';
            }elseif($data['data']['yiju'] == 'concailabour'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '劳务合同';
            }elseif($data['data']['yiju'] == 'labourshen'){
                $yijuidInfo = LabourShenModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '劳务进度款申报';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'labourjie'){
                $yijuidInfo = LabourJieModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '劳务完工结算';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'labouryong'){
                $yijuidInfo = LabourYongModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '劳务用工单';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'labourling'){
                $yijuidInfo = LabourLingModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '零星劳务单';
            }elseif($data['data']['yiju'] == 'concaidevice'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '租赁合同';
            }elseif($data['data']['yiju'] == 'devicejie'){
                $yijuidInfo = DeviceJieModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '租赁结算';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'devicelin'){
                $yijuidInfo = DeviceLinModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '机械费登记';
            }elseif($data['data']['yiju'] == 'concaisubbao'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '分包合同';
            }elseif($data['data']['yiju'] == 'subbaoshen'){
                $yijuidInfo = SubbaoShenModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '分包进度款申报';
            }elseif($data['data']['yiju'] == 'subbaojie'){
                $yijuidInfo = SubbaoJieModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '分包完工结算';
            }elseif($data['data']['yiju'] == 'concaiqi'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '其他采购合同';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }
            $data['yujuinfo'] = $yijuidInfo;


            $data['budid'] = (new PaymentService())->get_payment_budid();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_engin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_engin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_engin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_engin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_income_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择关联备用金');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入还款金额');
                }

                $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
                if($jieidinfo){
                    $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
                    $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                    $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                    $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
                    $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
                    $daihuan = $accmoney - $difee - $huanfee;
                    if($param['money'] > $daihuan){
                        return outputJson(4000, '还款金额不能大于当前备用金的“剩余待还金额”');
                    }
                }

                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入还款说明');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择还款日期');
                }
            }



            $param['number'] = (new IncomeService())->create_income_huan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = IncomeHuanModel::insertGetId($param);

                (new FlowService())->save_bill('income_huan', $id);

                (new OptService())->relation_file($files,'income_huan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_huan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_huan', $id, '还款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new IncomeService())->create_income_huan_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('income_huan');
            $data['isadvan'] = (new FlowService())->check_isadvan('income_huan');
            $data['isnext'] = (new FlowService())->check_isnext('income_huan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择关联备用金');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入还款金额');
                }
                $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
                if($jieidinfo){
                    $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
                    $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                    $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                    $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
                    $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
                    $daihuan = $accmoney - $difee - $huanfee;
                    if($param['money'] > $daihuan){
                        return outputJson(4000, '还款金额不能大于当前备用金的“剩余待还金额”');
                    }
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入还款说明');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择还款日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                IncomeHuanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('income_huan', $param['id']);
                }else{
                    (new FlowService())->save_bill('income_huan', $param['id']);
                }

                (new OptService())->relation_file($files,'income_huan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_huan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_huan', $param['id'], '还款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeHuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $jieinfo = PaymentJieModel::get_info_by_pk($data['data']['jieid']);
            $jieinfo['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $jieinfo['incdaotype']);
            $data['jieinfo'] = $jieinfo;

            $data['files'] = (new OptService())->get_file('income_huan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('income_huan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('income_huan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('income_huan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }




            $param['number'] = (new PaymentService())->create_payment_day_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = PaymentDayModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['dayid'] = $id;
                    PaymentDayListModel::insert($vv);
                }

                (new FlowService())->save_bill('payment_day', $id);

                (new OptService())->relation_file($files,'payment_day',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_day', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_day', $id, '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_day_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_day');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_day');
            $data['isnext'] = (new FlowService())->check_isnext('payment_day');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_day(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入请款金额');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }

            Db::startTrans();
            try {
                PaymentDayModel::update($param);

                PaymentDayListModel::where(['dayid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dayid'] = $param['id'];
                    PaymentDayListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_day', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_day', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_day',$param['id']);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_day', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_day', $param['id'], '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;

            $list = PaymentDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_day', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_day');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_day',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_day');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
                if($param['isdi'] == '是'){
                    if(empty($param['jieid'])){
                        return outputJson(4000, '请选择关联备用金');
                    }
//                    if(empty($param['difee'])){
//                        return outputJson(4000, '请输入本次冲抵');
//                    }
//                    if($param['difee'] > $param['shenfee']){
//                        return outputJson(4000, '本次冲抵不能大于申请金额');
//                    }
//
//                    $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
//                    if($jieidinfo){
//                        $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
//                        $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
//                        $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
//                        $daihuan = $accmoney - $difee - $huanfee;
//                        if($param['difee'] > $daihuan){
//                            return outputJson(4000, '本次冲抵不能大于剩余待还');
//                        }
//                    }
                }else{
                    $param['jieid'] = 0;
                    $param['difee'] = 0;
                }
            }




            $param['number'] = (new PaymentService())->create_payment_bao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                $id = PaymentBaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['baoid'] = $id;
                    PaymentBaoListModel::insert($vv);
                }

                (new FlowService())->save_bill('payment_bao', $id);

                (new OptService())->relation_file($files,'payment_bao',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_bao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_bao', $id, '项目费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_bao_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_bao');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_bao');
            $data['isnext'] = (new FlowService())->check_isnext('payment_bao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_all_xiala();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
                if($param['isdi'] == '是'){
                    if(empty($param['jieid'])){
                        return outputJson(4000, '请选择关联备用金');
                    }
//                    if(empty($param['difee'])){
//                        return outputJson(4000, '请输入本次冲抵');
//                    }
//
//                    if($param['difee'] > $param['shenfee']){
//                        return outputJson(4000, '本次冲抵不能大于申请金额');
//                    }
//
//                    $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
//                    if($jieidinfo){
//                        $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
//                        $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
//                        $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
//                        $daihuan = $accmoney - $difee - $huanfee;
//                        if($param['difee'] > $daihuan){
//                            return outputJson(4000, '本次冲抵不能大于剩余待还');
//                        }
//                    }
                }else{
                    $param['jieid'] = 0;
                    $param['difee'] = 0;
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                PaymentBaoModel::update($param);

                PaymentBaoListModel::where(['baoid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['baoid'] = $param['id'];
                    PaymentBaoListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_bao', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_bao', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_bao',$param['id']);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_bao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_bao', $param['id'], '项目费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = PaymentBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;

            $jieidinfo = PaymentJieModel::where(['id'=>$data['data']['jieid']])->find();
            if($jieidinfo){
                $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
                $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
                $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
                $jieidinfo['daihuan'] = $accmoney - $difee - $huanfee;
            }
            $data['jieidinfo'] = $jieidinfo;

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_bao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_bao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_bao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_bao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){

                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
                if($param['isdi'] == '是'){
                    if(empty($param['jieid'])){
                        return outputJson(4000, '请选择关联备用金');
                    }
//                    if(empty($param['difee'])){
//                        return outputJson(4000, '请输入本次冲抵');
//                    }
//
//                    if($param['difee'] > $param['shenfee']){
//                        return outputJson(4000, '本次冲抵不能大于申请金额');
//                    }
//
//                    $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
//                    if($jieidinfo){
//                        $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
//                        $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
//                        $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
//                        $daihuan = $accmoney - $difee - $huanfee;
//                        if($param['difee'] > $daihuan){
//                            return outputJson(4000, '本次冲抵不能大于剩余待还');
//                        }
//                    }
                }else{
                    $param['jieid'] = 0;
                    $param['difee'] = 0;
                }
            }


            $param['number'] = (new PaymentService())->create_payment_gong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                $id = PaymentGongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gongid'] = $id;
                    PaymentGongListModel::insert($vv);
                }

                (new FlowService())->save_bill('payment_gong', $id);

                (new OptService())->relation_file($files,'payment_gong',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_gong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_gong', $id, '公司费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['budid_name'] = '公司费用';
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_gong_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_gong');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_gong');
            $data['isnext'] = (new FlowService())->check_isnext('payment_gong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){

                if(empty($param['budid'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incqing'])){
                    return outputJson(4000, '请选择发票情况');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入付款对象');
                }
                if($param['isdi'] == '是'){
                    if(empty($param['jieid'])){
                        return outputJson(4000, '请选择关联备用金');
                    }
//                    if(empty($param['difee'])){
//                        return outputJson(4000, '请输入本次冲抵');
//                    }
//
//                    if($param['difee'] > $param['shenfee']){
//                        return outputJson(4000, '本次冲抵不能大于申请金额');
//                    }
//
//                    $jieidinfo = PaymentJieModel::where(['id'=>$param['jieid']])->find();
//                    if($jieidinfo){
//                        $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
//                        $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
//                        $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
//                        $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
//                        $daihuan = $accmoney - $difee - $huanfee;
//                        if($param['difee'] > $daihuan){
//                            return outputJson(4000, '本次冲抵不能大于剩余待还');
//                        }
//                    }
                }else{
                    $param['jieid'] = 0;
                    $param['difee'] = 0;
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                PaymentGongModel::update($param);

                PaymentGongListModel::where(['gongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gongid'] = $param['id'];
                    PaymentGongListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_gong', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_gong', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_gong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_gong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_gong', $param['id'], '公司费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentGongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();

            $list = PaymentGongListModel::where(['gongid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;

            $jieidinfo = PaymentJieModel::where(['id'=>$data['data']['jieid']])->find();
            if($jieidinfo){
                $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
                $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
                $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
                $jieidinfo['daihuan'] = $accmoney - $difee - $huanfee;
            }
            $data['jieidinfo'] = $jieidinfo;




            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_gong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_gong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_gong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_gong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_payment_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['incyong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入申请金额');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入借支人');
                }
            }




            $param['number'] = (new PaymentService())->create_payment_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = PaymentJieModel::insertGetId($param);

                (new FlowService())->save_bill('payment_jie', $id);

                (new OptService())->relation_file($files,'payment_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_jie', $id, '备用金申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['gonger'] = $this->userinfo['realname'];
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_jie_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('payment_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('payment_jie');
            $data['isnext'] = (new FlowService())->check_isnext('payment_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['incyong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入申请金额');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入借支人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                PaymentJieModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('payment_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('payment_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'payment_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_jie', $param['id'], '备用金申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('payment_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('payment_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('payment_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['money'])){
                    return outputJson(4000, '请输入开票金额');
                }
                if(empty($param['duier'])){
                    return outputJson(4000, '请选择受票单位');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }
            }


            $param['number'] = (new PiaoService())->create_piao_kai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = PiaokaiModel::insertGetId($param);

                (new FlowService())->save_bill('piao_kai', $id);

                (new OptService())->relation_file($files,'piao_kai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_kai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_kai', $id, '开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['number'] = (new PiaoService())->create_piao_kai_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_kai');
            $data['isnext'] = (new FlowService())->check_isnext('piao_kai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['money'])){
                    return outputJson(4000, '请输入开票金额');
                }
                if(empty($param['duier'])){
                    return outputJson(4000, '请选择受票单位');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                PiaokaiModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_kai', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_kai', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_kai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_kai', $param['id'], '开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            if($engininfo){
                $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('name');
                $data['engininfo'] = $engininfo;

            }

            $data['data']['kaier_info'] = (new UserService())->get_users_part_arr($data['data']['kaier'], 'dept');
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_kai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_kai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_piao_tax(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择预缴月份');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择发生日期');
                }
            }


            $param['number'] = (new PiaoService())->create_piao_tax_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预缴明细');
                }
            }


            Db::startTrans();
            try {
                $id = PiaoTaxModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['taxid'] = $id;
                        PiaoTaxListModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('piao_tax', $id);

                (new OptService())->relation_file($files,'piao_tax',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_tax', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_tax', $id, '税费预缴');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['ratetype'] = (new OptService())->get_base_info('cai_ratetype');
            $data['number'] = (new PiaoService())->create_piao_tax_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('piao_tax');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_tax');
            $data['isnext'] = (new FlowService())->check_isnext('piao_tax');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_tax(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择预缴月份');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择发生日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预缴明细');
                }
            }


            Db::startTrans();
            try {
                PiaoTaxModel::update($param);

                PiaoTaxListModel::where(['taxid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['taxid'] = $param['id'];
                        PiaoTaxListModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_tax', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_tax', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_tax',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_tax', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_tax', $param['id'], '税费预缴');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoTaxModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = PiaoTaxListModel::where(['taxid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $data['ratetype'] = (new OptService())->get_base_info('cai_ratetype');
            $data['files'] = (new OptService())->get_file('piao_tax', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_tax');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_tax',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_tax');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    //##############################################  证书  ##############################################
    //##############################################  证书  ##############################################
    public function add_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $useid = CertUseModel::insertGetId($param);

                foreach ($list as $item){
                    $item['useid'] = $useid;
                    CertUseListModel::insert($item);
                }

                (new FlowService())->save_bill('cert_use', $useid);

                (new OptService())->relation_file($files,'cert_use',$useid);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_use', $useid);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_use', $useid, '报建用证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$useid]);
        }else{
            $data['place'] = CertModel::get_place();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_use');
            $data['isnext'] = (new FlowService())->check_isnext('cert_use');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');


            Db::startTrans();
            try {
                CertUseModel::update($param);

                CertUseListModel::where('useid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['useid'] = $param['id'];
                    CertUseListModel::insert($item);
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_use', $param['id']);

                }else{
                    (new FlowService())->save_bill('cert_use', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_use',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_use', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_use', $param['id'], '用证申请');
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertUseModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;

            $list = CertUseListModel::where(['useid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $certInfo = CertModel::where(['id'=>$item['certid']])->find();
                $item['name'] = $certInfo['name'];
                $item['number'] = $certInfo['number'];
                $item['major_name'] = str_replace('[]', ',', $certInfo['major']);
                $item['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                $item['opt_name'] = $item['opt'] > 0 ? '在用' : '停用';
            }
            $data['list'] = $list;

            $data['place'] = CertModel::get_place();

            $data['files'] = (new OptService())->get_file('cert_use', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_use',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_use');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_use_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['bidid'=>$param['bidid']])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $useid = CertUseBidModel::insertGetId($param);

                foreach ($list as $item){
                    $item['useid'] = $useid;
                    CertUseBidListModel::insert($item);
                }

                (new FlowService())->save_bill('cert_use_bid', $useid);

                (new OptService())->relation_file($files,'cert_use_bid',$useid);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_use_bid', $useid);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_use_bid', $useid, '投标用证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$useid]);
        }else{
            $data['place'] = CertModel::get_place();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_use_bid');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_use_bid');
            $data['isnext'] = (new FlowService())->check_isnext('cert_use_bid');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_use_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['bidid'=>$param['bidid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');


            Db::startTrans();
            try {
                CertUseBidModel::update($param);

                CertUseBidListModel::where('useid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['useid'] = $param['id'];
                    CertUseBidListModel::insert($item);
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_use_bid', $param['id']);

                }else{
                    (new FlowService())->save_bill('cert_use_bid', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_use_bid',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_use_bid', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_use_bid', $param['id'], '投标申请');
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertUseBidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = BidModel::where(['id'=>$data['data']['bidid']])->find();
            $data['engininfo'] = $engininfo;

            $list = CertUseBidListModel::where(['useid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $certInfo = CertModel::where(['id'=>$item['certid']])->find();
                $item['name'] = $certInfo['name'];
                $item['number'] = $certInfo['number'];
                $item['major_name'] = str_replace('[]', ',', $certInfo['major']);
                $item['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                $item['opt_name'] = $item['opt'] > 0 ? '在用' : '停用';
            }
            $data['list'] = $list;

            $data['place'] = CertModel::get_place();

            $data['files'] = (new OptService())->get_file('cert_use_bid', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_use_bid');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_use_bid',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_use_bid');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = CertBorrowModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertBorrowListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_borrow',$id);
                (new FlowService())->save_bill('cert_borrow', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_borrow', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_borrow', $id, '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_borrow');
            $data['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                CertBorrowModel::update($param);

                CertBorrowListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertBorrowListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_borrow',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_borrow', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_borrow', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id'], '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertBorrowModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertBorrowListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = CertPersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['major_name'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['major_name'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_company_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_company_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$item['chanid_name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_borrow', $data['data']['id']);
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_borrow',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = CertDownModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertDownListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_down',$id);
                (new FlowService())->save_bill('cert_down', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_down', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_down', $id, '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_down');
            $data['isnext'] = (new FlowService())->check_isnext('cert_down');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                CertDownModel::update($param);

                CertDownListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertDownListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_down',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_down', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_down', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_down', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_down', $param['id'], '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertDownModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertDownListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = CertPersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_company_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_company_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$item['chanid_name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_down', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_down',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_down');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




    //##############################################  项目  ##############################################
    //##############################################  项目  ##############################################
    public function add_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入协议名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择协议年份');
                }
                if(empty($param['dutyer'])){
                    return outputJson(4000, '请选择内包责任人');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }

            }

            $param['number'] = (new ContractService())->create_contract_nei_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入费用设置');
                }

            }

            foreach ($param as $kk=>$vv){
                if(strpos($kk,'feiname') !== false){
                    unset($param[$kk]);
                }
            }


            Db::startTrans();
            try {
                $id = ContractNeiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['neiid'] = $id;
                        ContractNeiListModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('contract_nei', $id);
                (new OptService())->relation_file($files,'contract_nei',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $id, '内包协议');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new ContractService())->create_contract_nei_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_nei');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_nei');
            $data['isnext'] = (new FlowService())->check_isnext('contract_nei');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['feitype'] = (new ContractService())->get_contract_nei_feitype();
            $data['feiname'] = (new OptService())->get_base_info('engin_nei_feiname');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入协议名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择协议年份');
                }
                if(empty($param['dutyer'])){
                    return outputJson(4000, '请选择内包责任人');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入费用设置');
                }

            }


            foreach ($param as $kkk=>$vvv){
                if(strpos($kkk, 'feiname') !== false){
                    unset($param[$kkk]);
                }
            }

            Db::startTrans();
            try {
                ContractNeiModel::update($param);


                ContractNeiListModel::where(['neiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['neiid'] = $param['id'];
                        ContractNeiListModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_nei', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_nei', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_nei',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $param['id'], '内包协议');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractNeiModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['feitype'] = (new ContractService())->get_contract_nei_feitype();
            $data['feiname'] = (new OptService())->get_base_info('engin_nei_feiname');
            $data['files'] = (new OptService())->get_file('contract_nei', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_nei');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_nei',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_nei');


            $list = ContractNeiListModel::where(['neiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $item['feiname']);
                $item['feitype_name'] = (new ContractService())->get_contract_nei_feitype_name($item['feitype']);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '项目名称与主合同关联项目不匹配，请重新选择');
                }
            }


            $param['number'] = (new ContractService())->create_contract_bu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ContractBuModel::insertGetId($param);

                (new FlowService())->save_bill('contract_bu', $id);
                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bu', $id, '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new ContractService())->create_contract_bu_number();
            $shiid = $request->param('shiid', 0, 'int');
            $shiinfo = ContractShiModel::where(['id'=>$shiid])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
                $data['shiinfo'] = $shiinfo;

                $engininfo = EnginModel::get_info_by_pk($shiinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }


            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bu');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBuModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '项目名称与主合同关联项目不匹配，请重新选择');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                ContractBuModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bu', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bu', $param['id']);
                }

                (new EnginRelevan())->vate_engin_shimoney($old['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bu', $param['id'], '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shiinfo = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
                $data['shiinfo'] = $shiinfo;

                $engininfo = EnginModel::get_info_by_pk($shiinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }


            $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
            }

            $param['number'] = (new ContractService())->create_contract_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }





            Db::startTrans();
            try {
                $id = ContractCaiModel::insertGetId($param);

                $isbudget = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();

                $kknum = 1;
                foreach($list as $key=>$vv){
                    if($isbudget){
                        $bglist = EnginBudgetListModel::where(['id'=>$vv['listid']])->find();
                        $zhannum = ContractCaiListModel::alias('list')
                            ->join('contract_cai cai','cai.id = list.caiid')
                            ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                            ->where('cai.status','in',[0,1])
                            ->where(['list.listid'=>$vv['listid']])
                            ->sum('list.num');
                        $shengnum = $bglist['num'] - $zhannum;
                        if($vv['num'] > $shengnum){
                            throw new Exception("第".$kknum."行采购数量已超预算剩余量");
                        }
                    }
                    $kknum++;
                }

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    ContractCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_cai', $id);

                (new OptService())->relation_file($files,'contract_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai', $id, '采购合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new ContractService())->create_contract_cai_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            $data['subarr'] = (new BudgetService())->get_budsubquan_all();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_cai');
            $data['isnext'] = (new FlowService())->check_isnext('contract_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractCaiModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }

            Db::startTrans();
            try {
                ContractCaiModel::update($param);


                ContractCaiListModel::where(['caiid'=>$param['id']])->delete();

                $isbudget = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$old['enginid']])->find();
                $kknum = 1;
                foreach($list as $key=>$vv){
                    if($isbudget){
                        $bglist = EnginBudgetListModel::where(['id'=>$vv['listid']])->find();
                        $zhannum = ContractCaiListModel::alias('list')
                            ->join('contract_cai cai','cai.id = list.caiid')
                            ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                            ->where('cai.status','in',[0,1])
                            ->where(['list.listid'=>$vv['listid']])
                            ->sum('list.num');
                        $shengnum = $bglist['num'] - $zhannum;
                        if($vv['num'] > $shengnum){
                            throw new Exception("第".$kknum."行采购数量已超预算剩余量");
                        }
                    }
                    $kknum++;
                }

                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    ContractCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai', $param['id'], '采购合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');

            $data['data']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');

            $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['subid'] = 0;
                $item['guiname'] = '';
                $item['guiname'] = '';
                $listinfo = EnginBudgetListModel::where(['id'=>$item['listid']])->find();
                if($listinfo){
                    $item['subid'] = $listinfo['subid'];
                    $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                    $item['guiname'] = $listinfo['name'];
                }
            }
            $data['list'] = $list;
            $data['subarr'] = (new BudgetService())->get_budsubquan_all();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经办人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '项目名称与合同名称关联项目不匹配，请重新选择');
                }


                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }


            $cai_money = ContractCaiModel::where(['id'=>$param['caiid']])->value('money');

            $param['yuanfee'] = $cai_money;


            $param['number'] = (new ContractService())->create_contract_bian_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }
            }



            Db::startTrans();
            try {
                $id = ContractBianModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['bianid'] = $id;
                    ContractBianListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_bian', $id);
                (new OptService())->relation_file($files,'contract_bian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bian', $id, '合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $caiid = $request->param('caiid', 0, 'int');


            $caiinfo = ContractCaiModel::where(['id'=>$caiid])->find();
            if($caiinfo){
                $data['caiinfo'] = $caiinfo;

                $engininfo = EnginModel::get_info_by_pk($caiinfo['enginid']);
                if($engininfo){
                    $engininfo['budgetid'] = EnginBudgetModel::where(['enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');
                }
                $data['engininfo'] = $engininfo;
            }



            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new ContractService())->create_contract_bian_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bian');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBianModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经办人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '项目名称与合同名称关联项目不匹配，请重新选择');
                }

                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }


            $cai_money = ContractCaiModel::where(['id'=>$param['caiid']])->value('money');

            $param['yuanfee'] = $cai_money;


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }
            }


            Db::startTrans();
            try {
                ContractBianModel::update($param);


                ContractBianListModel::where(['bianid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['bianid'] = $param['id'];
                    ContractBianListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bian', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bian', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_bian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bian', $param['id'], '合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $caiinfo = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $data['caiinfo'] = $caiinfo;
            $engininfo = EnginModel::get_info_by_pk($caiinfo['enginid']);
            $data['engininfo'] = $engininfo;
            $data['data']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');

            $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['subname'] = '';
                $item['guiname'] = '';
                $listinfo = EnginBudgetListModel::where(['id'=>$item['subid']])->find();
                if($listinfo){
                    $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                    $item['guiname'] = $listinfo['name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }

                $engin = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engin['business'] == 2){
                    if(empty($param['neiid'])){
                        return outputJson(4000, '请选择内包协议');
                    }
                }

                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包方)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包方(乙方)');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

            }


            $param['number'] = (new ContractService())->create_contract_shi_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                $id = ContractShiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $id;
                        ContractShiGongModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('contract_shi', $id);

                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_shi',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi', $id, '施工合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new ContractService())->create_contract_shi_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['zhilevel'] = (new ContractService())->get_contract_shi_zhilevel();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_shi');
            $data['isnext'] = (new FlowService())->check_isnext('contract_shi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }

                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }

                $engin = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engin['business'] == 2){
                    if(empty($param['neiid'])){
                        return outputJson(4000, '请选择内包协议');
                    }
                }

                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包方)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包方(乙方)');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                ContractShiModel::update($param);

                ContractShiGongModel::where(['shiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $param['id'];
                        ContractShiGongModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_shi', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_shi', $param['id']);
                }

                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi', $param['id'], '施工合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
            $engininfo['chanid_name'] = DeptModel::get_name_by_pk($engininfo['chanid']);
            $engininfo['man_xiang_name'] = UserModel::get_realname_by_pk($engininfo['man_xiang']);
            $data['engininfo'] = $engininfo;

            $data['data']['neiid_name'] = ContractNeiModel::where(['id'=>$data['data']['neiid']])->value('name');
            $data['data']['chenger_info'] = (new UserService())->get_users_part_arr($data['data']['chenger'], 'dept');



            $list = ContractShiGongModel::where(['shiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['zhilevel'] = (new ContractService())->get_contract_shi_zhilevel();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_shi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_shi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }



            $param['number'] = (new DeviceService())->create_device_jin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进场明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceJinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jinid'] = $id;
                    DeviceJinListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_jin', $id);

                (new OptService())->relation_file($files,'device_jin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_jin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_jin', $id, '进场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_jin_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_jin');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_jin');
            $data['isnext'] = (new FlowService())->check_isnext('device_jin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进场明细');
                }
            }

            Db::startTrans();
            try {
                DeviceJinModel::update($param);

                DeviceJinListModel::where(['jinid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jinid'] = $param['id'];
                    DeviceJinListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_jin', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_jin', $param['id']);
                }

                (new OptService())->relation_file($files,'device_jin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_jin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_jin', $param['id'], '进场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceJinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = DeviceJinListModel::where(['jinid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_jin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_jin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_jin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_jin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_device_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }



            $param['number'] = (new DeviceService())->create_device_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DeviceJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_jie', $id);

                (new OptService())->relation_file($files,'device_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_jie', $id, '租赁结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_jie_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_jie');
            $data['isnext'] = (new FlowService())->check_isnext('device_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }



            Db::startTrans();
            try {
                DeviceJieModel::update($param);

                DeviceJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DeviceJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'device_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_jie', $param['id'], '租赁结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = DeviceJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_device_lin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
            }




            $param['number'] = (new DeviceService())->create_device_lin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入登记明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceLinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['linid'] = $id;
                    if(is_array($vv['dts'])){
                        $vv['dts'] = implode(' - ', $vv['dts']);
                    }
                    DeviceLinListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_lin', $id);

                (new OptService())->relation_file($files,'device_lin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_lin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_lin', $id, '临时机械登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_lin_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_lin');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_lin');
            $data['isnext'] = (new FlowService())->check_isnext('device_lin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_lin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入登记明细');
                }
            }


            Db::startTrans();
            try {
                DeviceLinModel::update($param);

                DeviceLinListModel::where(['linid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['linid'] = $param['id'];
                    if(is_array($vv['dts'])){
                        $vv['dts'] = implode(' - ', $vv['dts']);
                    }
                    DeviceLinListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_lin', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_lin', $param['id']);
                }

                (new OptService())->relation_file($files,'device_lin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_lin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_lin', $param['id'], '临时机械登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceLinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = DeviceLinListModel::where(['linid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['dtsarr'] = explode(' - ', $item['dts']);
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_lin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_lin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_lin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_lin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new EnginService())->check_engin_name($param['name'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if($param['business'] == 2){
                    if(empty($param['lian_unit'])){
                        return outputJson(4000, '请选择联营方');
                    }
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择项目状态');
                }
                if($param['state'] == 0){
                    $param['actualdt'] = '';
                    $param['filishdt'] = '';
                }
                if($param['state'] == 3){
                    $param['filishdt'] = '';
                }

                if($param['business'] == 2){
                    if(empty($param['lian_unit'])){
                        return outputJson(4000, '请选择联营方');
                    }
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginModel::insertGetId($param);

                (new FlowService())->save_bill('engin', $id);

                (new OptService())->relation_file($files,'engin',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin', $id, '项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['state'] = (new EnginService())->get_engin_state();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin');
            $data['isnext'] = (new FlowService())->check_isnext('engin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new EnginService())->check_engin_name($param['name'], $param['id'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if($param['business'] == 2){
                    if(empty($param['lian_unit'])){
                        return outputJson(4000, '请选择联营方');
                    }
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择所属单位');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择项目状态');
                }
                if($param['state'] == 0){
                    $param['actualdt'] = '';
                    $param['filishdt'] = '';
                }
                if($param['state'] == 3){
                    $param['filishdt'] = '';
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin', $param['id']);

                }else{
                    (new FlowService())->save_bill('engin', $param['id']);
                }

                (new OptService())->relation_file($files,'engin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin', $param['id'], '项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['man_xiang_info'] = (new UserService())->get_users_part_arr($data['data']['man_xiang'], 'user');
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['state'] = (new EnginService())->get_engin_state();

            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_budget(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入预算主题');
                }

                $exist = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下存在预算，请勿重复添加');
                }
            }

            $param['number'] = (new BudgetService())->create_engin_budget_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预算明细');
                }
            }

            $subSort = array_column($list,'subid');
            array_multisort($subSort,SORT_DESC,$list);

            Db::startTrans();
            try {
                $id = EnginBudgetModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['budid'] = $id;
                    EnginBudgetListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_budget', $id);

                (new OptService())->relation_file($files,'engin_budget',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_budget', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_budget', $id, '成本预算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BudgetService())->create_engin_budget_number();
            $data['bianer'] = $this->userinfo['realname'];
            $data['biandt'] = date("Y-m-d");
            $data['navarr'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>0])
                ->where('name','neq','公司费用')
                ->select()->toArray();


            foreach ($data['navarr'] as &$item){
                $item['children'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where('id='.$item['id'].' or topid='.$item['id'])->column('id');
            }

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_budget');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_budget');
            $data['isnext'] = (new FlowService())->check_isnext('engin_budget');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_budget(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginBudgetModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入预算主题');
                }

                $exist = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下存在预算，请勿重复添加');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预算明细');
                }
            }


            $subSort = array_column($list,'subid');
            array_multisort($subSort,SORT_DESC,$list);



            Db::startTrans();
            try {
                EnginBudgetModel::update($param);
                EnginBudgetListModel::where(['budid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['budid'] = $param['id'];
                    EnginBudgetListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_budget', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_budget', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_budget',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_budget', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_budget', $param['id'], '成本预算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBudgetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = EnginBudgetListModel::where(['budid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$vvv){
                $vvv['subname'] = (new BudgetService())->get_subid_quanname($vvv['subid']);
            }
            $data['list'] = $list;

            $data['navarr'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>0])
                ->where('name','neq','公司费用')
                ->select()->toArray();
            foreach ($data['navarr'] as &$item){
                $item['children'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where('id='.$item['id'].' or topid='.$item['id'])->column('id');
            }
            $data['files'] = (new OptService())->get_file('engin_budget', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_budget');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_budget',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_budget');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_budbian(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zhuname'])){
                    return outputJson(4000, '请输入变更主题');
                }
                if(empty($param['budgetid'])){
                    return outputJson(4000, '请选择预算主题');
                }

                $bud_enginid = EnginBudgetModel::where(['id'=>$param['budgetid']])->value('enginid');
                if($param['enginid'] != $bud_enginid){
                    return outputJson(4000, '项目名称与预算主题关联项目不匹配，请重新选择');
                }

                $exist = EnginBudbianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'budgetid'=>$param['budgetid']])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }

            }

            $param['number'] = (new BudgetService())->create_engin_budbian_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $tiaolist = array_filter($param['tiaolist']);
            unset($param['tiaolist']);
            $zenglist = array_filter($param['zenglist']);
            unset($param['zenglist']);

            if($param['isturn'] > 0){
                if($param['type'] == 1 || $param['type'] == 3){
                    if(count($tiaolist) < 1){
                        return outputJson(4000, '请输入调整明细');
                    }
                }
                if($param['type'] == 2 || $param['type'] == 3){
                    if(count($zenglist) < 1){
                        return outputJson(4000, '请输入增项明细');
                    }

                    foreach ($zenglist as $kkk=>$vvv){
                        $zengExist = EnginBudgetListModel::where(['budid'=>$param['budgetid'],'subid'=>$vvv['subid']])
                            ->where(['name'=>$vvv['name'],'unit'=>$vvv['unit']])->find();
                        if($zengExist){
                            return outputJson(4000, '第'.$kkk.'行【成本科目+名称及规格+单位】在原有预算明细中已存在');
                        }
                    }
                }
            }


            Db::startTrans();
            try {
                $id = EnginBudbianModel::insertGetId($param);

                if($param['type'] == 1 || $param['type'] == 3){
                    foreach($tiaolist as $vv){
                        $vv['budid'] = $id;
                        EnginBudbianListModel::insert($vv);
                    }
                }

                if($param['type'] == 2 || $param['type'] == 3){
                    foreach($zenglist as $vv){
                        $vv['budid'] = $id;
                        EnginBudbianListModel::insert($vv);
                    }
                }

                (new FlowService())->save_bill('engin_budbian', $id);
                (new OptService())->relation_file($files,'engin_budbian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_budbian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_budbian', $id, '成本预算变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $budgetid = $request->param('budgetid', 0, 'int');

            $budgetinfo = EnginBudgetModel::get_info_by_pk($budgetid);
            if($budgetinfo){
                $data['budgetinfo'] = $budgetinfo;

                $engininfo = EnginModel::get_info_by_pk($budgetinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_budbian');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_budbian');
            $data['isnext'] = (new FlowService())->check_isnext('engin_budbian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['number'] = (new BudgetService())->create_engin_budbian_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_budbian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zhuname'])){
                    return outputJson(4000, '请输入变更主题');
                }
                if(empty($param['budgetid'])){
                    return outputJson(4000, '请选择预算主题');
                }

                $bud_enginid = EnginBudgetModel::where(['id'=>$param['budgetid']])->value('enginid');
                if($param['enginid'] != $bud_enginid){
                    return outputJson(4000, '项目名称与预算主题关联项目不匹配，请重新选择');
                }

                $exist = EnginBudbianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'budgetid'=>$param['budgetid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $tiaolist = array_filter($param['tiaolist']);
            unset($param['tiaolist']);
            $zenglist = array_filter($param['zenglist']);
            unset($param['zenglist']);

            if($param['isturn'] > 0){
                if($param['type'] == 1 || $param['type'] == 3){
                    if(count($tiaolist) < 1){
                        return outputJson(4000, '请输入调整明细');
                    }
                }
                if($param['type'] == 2 || $param['type'] == 3){
                    if(count($zenglist) < 1){
                        return outputJson(4000, '请输入增项明细');
                    }

                    foreach ($zenglist as $kkk=>$vvv){
                        $zengExist = EnginBudgetListModel::where(['budid'=>$param['budgetid'],'subid'=>$vvv['subid']])
                            ->where(['name'=>$vvv['name'],'unit'=>$vvv['unit']])->find();
                        if($zengExist){
                            return outputJson(4000, '第'.$kkk.'行【成本科目+名称及规格+单位】在原有预算明细中已存在');
                        }
                    }
                }
            }


            Db::startTrans();
            try {
                EnginBudbianModel::update($param);


                EnginBudbianListModel::where(['budid'=>$param['id']])->delete();
                if($param['type'] == 1 || $param['type'] == 3){
                    foreach($tiaolist as $vv){
                        $vv['budid'] = $param['id'];
                        EnginBudbianListModel::insert($vv);
                    }
                }

                if($param['type'] == 2 || $param['type'] == 3){
                    foreach($zenglist as $vv){
                        $vv['budid'] = $param['id'];
                        EnginBudbianListModel::insert($vv);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_budbian', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_budbian', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_budbian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_budbian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_budbian', $param['id'], '成本预算变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBudbianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $budgetinfo = EnginBudgetModel::where(['id'=>$data['data']['budgetid']])->find();
            if($budgetinfo){
                $data['budgetinfo'] = $budgetinfo;

                $engininfo = EnginModel::get_info_by_pk($budgetinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }


            $tiaolist = EnginBudbianListModel::where(['budid'=>$data['data']['id']])->where('listid','gt',0)->select()->toArray();
            $data['tiaolist'] = $tiaolist;


            $zenglist = EnginBudbianListModel::where(['budid'=>$data['data']['id']])->where('listid','=',0)->select()->toArray();
            $data['zenglist'] = $zenglist;

            $data['files'] = (new OptService())->get_file('engin_budbian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_budbian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_budbian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_budbian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_chan_month($param['month'], $param['enginid'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }
            }


            $param['number'] = (new SceneService())->create_engin_chan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }
            }


            Db::startTrans();
            try {
                $id = EnginChanModel::insertGetId($param);

                foreach($list as $item){
                    $item['chanid'] = $id;
                    EnginChanListModel::insert($item);
                }

                (new FlowService())->save_bill('engin_chan', $id);

                (new OptService())->relation_file($files,'engin_chan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_chan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_chan', $id, '产值填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{

            $data['number'] = (new SceneService())->create_engin_chan_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_chan');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_chan');
            $data['isnext'] = (new FlowService())->check_isnext('engin_chan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_chan_month($param['month'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }
            }


            Db::startTrans();
            try {
                EnginChanModel::update($param);

                EnginChanListModel::where(['chanid'=>$param['id']])->delete();
                foreach($list as $item){
                    $item['chanid'] = $param['id'];
                    EnginChanListModel::insert($item);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_chan', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_chan', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_chan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_chan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_chan', $param['id'], '产值填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = EnginChanListModel::where(['chanid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_chan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_chan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_chan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_chan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['tian'])){
                    return outputJson(4000, '请输入天气情况');
                }

                if((new SceneService())->check_engin_diary_dt($param['dt'], $param['enginid'])){
                    return outputJson(4000, '该项目日志日期已存在');
                }
            }


            $param['tianer'] = $this->userinfo['realname'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginDiaryModel::insertGetId($param);

                (new FlowService())->save_bill('engin_diary', $id);

                (new OptService())->relation_file($files,'engin_diary',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_diary', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_diary', $id, '施工日志');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['tianer'] = $this->userinfo['realname'];
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_diary');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_diary');
            $data['isnext'] = (new FlowService())->check_isnext('engin_diary');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['tian'])){
                    return outputJson(4000, '请输入天气情况');
                }

                if((new SceneService())->check_engin_diary_dt($param['dt'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目日志日期已存在');
                }
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginDiaryModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_diary', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_diary', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_diary',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_diary', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_diary', $param['id'], '施工日志');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginDiaryModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['files'] = (new OptService())->get_file('engin_diary', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_diary');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_diary',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_diary');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_junyan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['buture'])){
                    return outputJson(4000, '请选择建设性质');
                }
                if(empty($param['actualdt'])){
                    return outputJson(4000, '请选择开工日期');
                }
                if(empty($param['filishdt'])){
                    return outputJson(4000, '请选择竣工日期');
                }
                if(empty($param['yanjie'])){
                    return outputJson(4000, '请输入验收结论');
                }
                if(empty($param['yandt'])){
                    return outputJson(4000, '请选择验收日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = EnginJunyanModel::insertGetId($param);

                (new FlowService())->save_bill('engin_junyan', $id);
                (new OptService())->relation_file($files,'engin_junyan',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_junyan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_junyan', $id, '竣工验收单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['buture'] = (new EnginService())->get_engin_buture();
            $data['yanjie'] = (new EnginService())->get_engin_yanjie();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_junyan');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_junyan');
            $data['isnext'] = (new FlowService())->check_isnext('engin_junyan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_junyan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['buture'])){
                    return outputJson(4000, '请选择建设性质');
                }
                if(empty($param['actualdt'])){
                    return outputJson(4000, '请选择开工日期');
                }
                if(empty($param['filishdt'])){
                    return outputJson(4000, '请选择竣工日期');
                }
                if(empty($param['yanjie'])){
                    return outputJson(4000, '请输入验收结论');
                }
                if(empty($param['yandt'])){
                    return outputJson(4000, '请选择验收日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                EnginJunyanModel::update($param);



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_junyan', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_junyan', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_junyan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_junyan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_junyan', $param['id'], '竣工验收单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginJunyanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;
            $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
            }
            $data['shiinfo'] = $shiinfo;


            $data['buture'] = (new EnginService())->get_engin_buture();
            $data['yanjie'] = (new EnginService())->get_engin_yanjie();
            $data['files'] = (new OptService())->get_file('engin_junyan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_junyan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_junyan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_junyan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_junjie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['songdt'])){
                    return outputJson(4000, '请选择送审日期');
                }
                if(empty($param['songfee'])){
                    return outputJson(4000, '请输入送审金额');
                }
                if(empty($param['shendt'])){
                    return outputJson(4000, '请选择审定日期');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入审定金额');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);


            Db::startTrans();
            try {
                $id = EnginJunjieModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['jieid'] = $id;
                        EnginJunjieListModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('engin_junjie', $id);
                (new OptService())->relation_file($files,'engin_junjie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_junjie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_junjie', $id, '竣工结算单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_junjie');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_junjie');
            $data['isnext'] = (new FlowService())->check_isnext('engin_junjie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_junjie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['songdt'])){
                    return outputJson(4000, '请选择送审日期');
                }
                if(empty($param['songfee'])){
                    return outputJson(4000, '请输入送审金额');
                }
                if(empty($param['shendt'])){
                    return outputJson(4000, '请选择审定日期');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入审定金额');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);


            Db::startTrans();
            try {
                EnginJunjieModel::update($param);

                EnginJunjieListModel::where(['jieid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['jieid'] = $param['id'];
                        EnginJunjieListModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_junjie', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_junjie', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_junjie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_junjie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_junjie', $param['id'], '竣工结算单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginJunjieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;
            $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
            }
            $data['shiinfo'] = $shiinfo;
            $data['data']['shi_money'] = (new EnginService())->get_contract_shi_final_money($data['data']['enginid']);
            $data['data']['qian_money'] = VisaModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shemoney');
            $data['data']['chan_money'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('leijin');
            $data['data']['hui_money'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0])->sum('money');


            $list = EnginJunjieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_junjie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_junjie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_junjie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_junjie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_speed_month($param['month'], $param['enginid'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }

            }



            $param['number'] = (new SceneService())->create_engin_speed_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }
            }


            Db::startTrans();
            try {
                $id = EnginSpeedModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['speedid'] = $id;
                    EnginSpeedListModel::where(['zongid'=>$vv['zongid']])->update(['findt'=>$vv['findt']]);
                    EnginSpeedListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_speed', $id);
                (new OptService())->relation_file($files,'engin_speed',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_speed', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_speed', $id, '进度填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new SceneService())->create_engin_speed_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_speed');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_speed');
            $data['isnext'] = (new FlowService())->check_isnext('engin_speed');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_speed_month($param['month'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }

            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }

            }

            Db::startTrans();
            try {
                EnginSpeedModel::update($param);

                EnginSpeedListModel::where(['speedid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['speedid'] = $param['id'];
                    EnginSpeedListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_speed', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_speed', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_speed',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_speed', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_speed', $param['id'], '进度填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginSpeedModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $monthinfo = SpeedMonthModel::get_info_by_pk($data['data']['monthid']);
            $data['monthinfo'] = $monthinfo;


            $list = EnginSpeedListModel::where(['speedid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_speed', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_speed');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_speed',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_speed');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }


            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();
            $newparam['flowuser'] = $this->userinfo['realname'];
            $newparam['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['enginid'] = $param['enginid'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['enginid'] = $param['enginid'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';


            Db::startTrans();
            try {
                $id = EnginStampModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['stampid'] = $id;
                    EnginStampListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_stamp', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_stamp', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_stamp', $id, '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_stamp');
            $data['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }


            $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }

            }


            $newparam['id'] = $param['id'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['enginid'] = $param['enginid'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';

            Db::startTrans();
            try {
                EnginStampModel::update($newparam);

                EnginStampListModel::where(['stampid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['stampid'] = $param['id'];
                    EnginStampListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_stamp', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_stamp', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id'], '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['yongtypemob'] = $item['yongtype'];
                $item['yongtype'] = explode(',', $item['yongtype']);
                $item['yongtype_name'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_stamp',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_labour_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
            }



            $param['number'] = (new LabourService())->create_labour_shen_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入申报明细');
                }
            }



            Db::startTrans();
            try {
                $id = LabourShenModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shenid'] = $id;
                    LabourShenListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_shen', $id);

                (new OptService())->relation_file($files,'labour_shen',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_shen', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_shen', $id, '劳务进度款申报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new LabourService())->create_labour_shen_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_shen');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_shen');
            $data['isnext'] = (new FlowService())->check_isnext('labour_shen');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入申报明细');
                }
            }

            Db::startTrans();
            try {
                LabourShenModel::update($param);

                LabourShenListModel::where(['shenid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shenid'] = $param['id'];
                    LabourShenListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_shen', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_shen', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_shen',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_shen', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_shen', $param['id'], '劳务进度款申报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourShenModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = LabourShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_shen', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_shen');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_shen',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_shen');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_labour_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择劳务班组');
                }
            }


            $param['number'] = (new LabourService())->create_labour_ling_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入登记明细');
                }
            }



            Db::startTrans();
            try {
                $id = LabourLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    LabourLingListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_ling', $id);

                (new OptService())->relation_file($files,'labour_ling',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_ling', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_ling', $id, '零星劳务登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new LabourService())->create_labour_ling_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_ling');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_ling');
            $data['isnext'] = (new FlowService())->check_isnext('labour_ling');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择劳务班组');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入登记明细');
                }
            }

            Db::startTrans();
            try {
                LabourLingModel::update($param);

                LabourLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    LabourLingListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_ling', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_ling', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_ling',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_ling', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_ling', $param['id'], '零星劳务登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = LabourLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_ling', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_ling');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_ling',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_ling');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_labour_yong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['yongxing'])){
                    return outputJson(4000, '请选择用工性质');
                }
                if($param['yongxing'] == '劳务分包'){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择关联合同');
                    }
                    $param['gonger'] = ContractCaiModel::where(['id'=>$param['caiid']])->value('gonger');
                }else{
                    $param['caiid'] = 0;
                    $param['gonger'] = '';
                }


            }

            $param['number'] = (new LabourService())->create_labour_yong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用工信息');
                }
            }



            Db::startTrans();
            try {
                $id = LabourYongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['yongid'] = $id;
                    LabourYongListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_yong', $id);

                (new OptService())->relation_file($files,'labour_yong',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_yong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_yong', $id, '用工单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['number'] = (new LabourService())->create_labour_yong_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_yong');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_yong');
            $data['isnext'] = (new FlowService())->check_isnext('labour_yong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_yong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['yongxing'])){
                    return outputJson(4000, '请选择用工性质');
                }
                if($param['yongxing'] == '劳务分包'){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择关联合同');
                    }
                    $param['gonger'] = ContractCaiModel::where(['id'=>$param['caiid']])->value('gonger');
                }else{
                    $param['caiid'] = 0;
                    $param['gonger'] = '';
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用工信息');
                }
            }

            Db::startTrans();
            try {
                LabourYongModel::update($param);

                LabourYongListModel::where(['yongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['yongid'] = $param['id'];
                    LabourYongListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_yong', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_yong', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_yong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_yong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_yong', $param['id'], '用工单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourYongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['yongdtarr'] = explode(' - ',$data['data']['yongdt']);

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = LabourYongListModel::where(['yongid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $gonginfo = LabourGongModel::where(['id'=>$item['gongid']])->find();
                if($gonginfo){
                    $item['name'] = $gonginfo['name'];
                    $item['idcard'] = $gonginfo['idcard'];
                }
            }
            $data['list'] = $list;

            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['files'] = (new OptService())->get_file('labour_yong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_yong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_yong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_yong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_labour_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
            }


            $param['number'] = (new LabourService())->create_labour_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }



            Db::startTrans();
            try {
                $id = LabourJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    LabourJieListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_jie', $id);

                (new OptService())->relation_file($files,'labour_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_jie', $id, '劳务完工结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new LabourService())->create_labour_jie_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_jie');
            $data['isnext'] = (new FlowService())->check_isnext('labour_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }

            Db::startTrans();
            try {
                LabourJieModel::update($param);

                LabourJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    LabourJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_jie', $param['id'], '劳务完工结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = LabourJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_stuff_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }

            }



            $param['number'] = (new StuffService())->create_stuff_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购明细');
                }

            }


            Db::startTrans();
            try {
                $id = StuffCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    StuffCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('stuff_cai', $id);

                (new OptService())->relation_file($files,'stuff_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_cai', $id, '材料采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new StuffService())->create_stuff_cai_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('stuff_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('stuff_cai');
            $data['isnext'] = (new FlowService())->check_isnext('stuff_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购明细');
                }
            }


            Db::startTrans();
            try {
                StuffCaiModel::update($param);

                StuffCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    StuffCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('stuff_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('stuff_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'stuff_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_cai', $param['id'], '材料采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = StuffCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('stuff_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('stuff_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('stuff_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('stuff_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if(empty($param['rudt'])){
                    return outputJson(4000, '请选择入库日期');
                }
                if($param['ischu'] > 0 && empty($param['baner_chu'])){
                    return outputJson(4000, '请输入领料人');
                }
            }

            if($param['ishe'] == 2){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['danid']])->value('caiid');
            }


            $param['number'] = (new StuffService())->create_stuff_ru_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }

            }

            foreach ($list as $kk=>$vv){
                if($param['ishe'] == 1){
                    $henum = ContractCaiListModel::where(['id'=>$vv['listid']])->value('num');
                    $runum =  StuffRuListModel::alias('list')
                        ->join('stuff_ru ru','ru.id=list.ruid')
                        ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                        ->where('ru.status','in',[0,1])
                        ->where('ru.ishe','=',1)
                        ->where('list.listid','=',$vv['listid'])
                        ->sum('list.num');
                    $shengnum = $henum - $runum;
                    if((int)$vv['num'] > $shengnum){
                        return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$runum);
                    }
                }
                if($param['ishe'] == 2){
                    $henum = StuffCaiListModel::where(['id'=>$vv['listid']])->value('num');
                    $runum =  StuffRuListModel::alias('list')
                        ->join('stuff_ru ru','ru.id=list.ruid')
                        ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                        ->where('ru.status','in',[0,1])
                        ->where('ru.ishe','=',2)
                        ->where('list.listid','=',$vv['listid'])
                        ->sum('list.num');

                    $shengnum = $henum - $runum;
                    if((int)$vv['num'] > $shengnum){
                        return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$runum);
                    }
                }
            }

            Db::startTrans();
            try {
                $id = StuffRuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['ruid'] = $id;
                    StuffRuListModel::insert($vv);
                }

                (new FlowService())->save_bill('stuff_ru', $id);

                (new OptService())->relation_file($files,'stuff_ru',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_ru', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_ru', $id, '材料入库单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new StuffService())->create_stuff_ru_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('stuff_ru');
            $data['isadvan'] = (new FlowService())->check_isadvan('stuff_ru');
            $data['isnext'] = (new FlowService())->check_isnext('stuff_ru');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if(empty($param['rudt'])){
                    return outputJson(4000, '请选择入库日期');
                }
                if($param['ischu'] > 0 && empty($param['baner_chu'])){
                    return outputJson(4000, '请输入领料人');
                }
            }
            if($param['ishe'] == 2){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['danid']])->value('caiid');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }
            }


            Db::startTrans();
            try {
                StuffRuModel::update($param);

                StuffRuListModel::where(['ruid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['ruid'] = $param['id'];
                    StuffRuListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('stuff_ru', $param['id']);
                }else{
                    (new FlowService())->save_bill('stuff_ru', $param['id']);
                }

                (new OptService())->relation_file($files,'stuff_ru',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id'], '材料入库单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffRuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $data['danidinfo'] = StuffCaiModel::where(['id'=>$data['data']['danid']])->find();

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $list = StuffRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('stuff_ru', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('stuff_ru');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('stuff_ru',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('stuff_ru');

            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_stuff_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zhuname'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }

                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['number'] = (new StuffService())->create_stuff_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }

            Db::startTrans();
            try {
                $id = StuffJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    StuffJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('stuff_jie', $id);

                (new OptService())->relation_file($files,'stuff_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_jie', $id, '材料结算单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new StuffService())->create_stuff_jie_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('stuff_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('stuff_jie');
            $data['isnext'] = (new FlowService())->check_isnext('stuff_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['zhuname'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }

                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }


            Db::startTrans();
            try {
                StuffJieModel::update($param);

                StuffJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    StuffJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('stuff_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('stuff_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'stuff_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_jie', $param['id'], '材料结算单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

            $list = StuffJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('stuff_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('stuff_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('stuff_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('stuff_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_subbao_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '选择采购合同');
                }
            }



            $param['number'] = (new SubbaoService())->create_subbao_shen_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入申报明细');
                }
            }

            Db::startTrans();
            try {
                $id = SubbaoShenModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shenid'] = $id;
                    SubbaoShenListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('subbao_shen', $id);

                (new OptService())->relation_file($files,'subbao_shen',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'subbao_shen', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'subbao_shen', $id, '分包进度款申报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SubbaoService())->create_subbao_shen_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('subbao_shen');
            $data['isadvan'] = (new FlowService())->check_isadvan('subbao_shen');
            $data['isnext'] = (new FlowService())->check_isnext('subbao_shen');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_subbao_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '选择采购合同');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入申报明细');
                }
            }


            Db::startTrans();
            try {
                SubbaoShenModel::update($param);

                SubbaoShenListModel::where(['shenid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shenid'] = $param['id'];
                    SubbaoShenListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('subbao_shen', $param['id']);
                }else{
                    (new FlowService())->save_bill('subbao_shen', $param['id']);
                }

                (new OptService())->relation_file($files,'subbao_shen',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'subbao_shen', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'subbao_shen', $param['id'], '分包进度款申报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SubbaoShenModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = SubbaoShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('subbao_shen', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('subbao_shen');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('subbao_shen',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('subbao_shen');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_subbao_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '选择采购合同');
                }
            }




            $param['number'] = (new SubbaoService())->create_subbao_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }



            Db::startTrans();
            try {
                $id = SubbaoJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    SubbaoJieListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('subbao_jie', $id);

                (new OptService())->relation_file($files,'subbao_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'subbao_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'subbao_jie', $id, '分包完工结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SubbaoService())->create_subbao_jie_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('subbao_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('subbao_jie');
            $data['isnext'] = (new FlowService())->check_isnext('subbao_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_subbao_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '选择采购合同');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入结算明细');
                }
            }


            Db::startTrans();
            try {
                SubbaoJieModel::update($param);

                SubbaoJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    SubbaoJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('subbao_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('subbao_jie', $param['id']);
                }


                (new OptService())->relation_file($files,'subbao_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'subbao_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'subbao_jie', $param['id'], '分包完工结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SubbaoJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = SubbaoJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('subbao_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('subbao_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('subbao_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('subbao_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_visa(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['visatype'])){
                    return outputJson(4000, '请选择签证类别');
                }
                if($param['shemoney'] == ''){
                    return outputJson(4000, '请输入涉及金额');
                }
            }


            $param['number'] = (new SceneService())->create_visa_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = VisaModel::insertGetId($param);


                (new FlowService())->save_bill('visa', $id);

                (new OptService())->relation_file($files,'visa',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'visa', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'visa', $id, '工程签证单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new SceneService())->create_visa_number();
            $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('visa');
            $data['isadvan'] = (new FlowService())->check_isadvan('visa');
            $data['isnext'] = (new FlowService())->check_isnext('visa');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_visa(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['visatype'])){
                    return outputJson(4000, '请选择签证类别');
                }
                if($param['shemoney'] == ''){
                    return outputJson(4000, '请输入涉及金额');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                VisaModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('visa', $param['id']);
                }else{
                    (new FlowService())->save_bill('visa', $param['id']);
                }

                (new OptService())->relation_file($files,'visa',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'visa', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'visa', $param['id'], '工程签证单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = VisaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
            $data['files'] = (new OptService())->get_file('visa', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('visa');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('visa',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('visa');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    //##############################################  其他  ##############################################
    //##############################################  其他  ##############################################
    public function add_archive_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['diaotype'])){
                    return outputJson(4000, '请选择调动类型');
                }
                if(empty($param['diaodept'])){
                    return outputJson(4000, '请选择调至部门');
                }
                if(empty($param['diaogang'])){
                    return outputJson(4000, '请选择调至岗位');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择调动日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ArchiveDiaoModel::insertGetId($param);

                (new FlowService())->save_bill('archive_diao', $id);

                (new OptService())->relation_file($files,'archive_diao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'archive_diao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'archive_diao', $id, '调动');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['diaotype'] = (new OptService())->get_base_info('qi_archive_diaotype');
            $data['deptname'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['gangname'] = UserGangModel::get_name_by_ids($this->userinfo['gangid']);
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('archive_diao');
            $data['isadvan'] = (new FlowService())->check_isadvan('archive_diao');
            $data['isnext'] = (new FlowService())->check_isnext('archive_diao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_archive_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['diaotype'])){
                    return outputJson(4000, '请选择调动类型');
                }
                if(empty($param['diaodept'])){
                    return outputJson(4000, '请选择调至部门');
                }
                if(empty($param['diaogang'])){
                    return outputJson(4000, '请选择调至岗位');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择调动日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                ArchiveDiaoModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('archive_diao', $param['id']);
                }else{
                    (new FlowService())->save_bill('archive_diao', $param['id']);
                }

                (new OptService())->relation_file($files,'archive_diao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'archive_diao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'archive_diao', $param['id'], '调动');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ArchiveDiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['diaogang'] = explode(',', $data['data']['diaogang']);
            $data['data']['diaodept_info'] = (new UserService())->get_users_part_arr($data['data']['diaodept'], 'dept');

            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['diaotype'] = (new OptService())->get_base_info('qi_archive_diaotype');
            $data['files'] = (new OptService())->get_file('archive_diao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('archive_diao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('archive_diao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('archive_diao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_archive_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['outshidt'])){
                    return outputJson(4000, '请选择实际离职日期');
                }
                if(empty($param['jiaoer'])){
                    return outputJson(4000, '请选择工作交接人');
                }
                if(empty($param['jiaodt'])){
                    return outputJson(4000, '请选择交接时间');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $param['dept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $param['gang'] = UserGangModel::get_name_by_ids($this->userinfo['gangid'], '，');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            Db::startTrans();
            try {
                $id = ArchiveJiaoModel::insertGetId($param);
                foreach ($list as $item) {
                    $item['jiaoid'] = $id;
                    ArchiveJiaoListModel::insert($item);
                }


                (new FlowService())->save_bill('archive_jiao', $id);

                (new OptService())->relation_file($files,'archive_jiao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'archive_jiao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'archive_jiao', $id, '离职交接');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['gang'] = UserGangModel::get_name_by_ids($this->userinfo['gangid'], '，');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('archive_jiao');
            $data['isadvan'] = (new FlowService())->check_isadvan('archive_jiao');
            $data['isnext'] = (new FlowService())->check_isnext('archive_jiao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_archive_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['outshidt'])){
                    return outputJson(4000, '请选择实际离职日期');
                }
                if(empty($param['jiaoer'])){
                    return outputJson(4000, '请选择工作交接人');
                }
                if(empty($param['jiaodt'])){
                    return outputJson(4000, '请选择交接时间');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                ArchiveJiaoModel::update($param);
                ArchiveJiaoListModel::where(['jiaoid'=>$param['id']])->delete();
                foreach ($list as $item) {
                    $item['jiaoid'] = $param['id'];
                    ArchiveJiaoListModel::insert($item);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('archive_jiao', $param['id']);
                }else{
                    (new FlowService())->save_bill('archive_jiao', $param['id']);
                }

                (new OptService())->relation_file($files,'archive_jiao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'archive_jiao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'archive_jiao', $param['id'], '离职交接');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ArchiveJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['jiaoer_info'] = (new UserService())->get_users_part_arr($data['data']['jiaoer'], 'user');

            $list = ArchiveJiaoListModel::where(['jiaoid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $data['files'] = (new OptService())->get_file('archive_jiao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('archive_jiao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('archive_jiao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('archive_jiao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_archive_zhuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['intodt'])){
                    return outputJson(4000, '请选择入职日期');
                }
                if(empty($param['shidaodt'])){
                    return outputJson(4000, '请选择试用期到期日期');
                }
                if($param['gongfee'] == ''){
                    return outputJson(4000, '请输入试用期工资');
                }
                if(empty($param['zhuandt'])){
                    return outputJson(4000, '请选择申请转正日期');
                }
                if($param['zhuanfee'] == ''){
                    return outputJson(4000, '请输入转正工资');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ArchiveZhuanModel::insertGetId($param);

                (new FlowService())->save_bill('archive_zhuan', $id);

                (new OptService())->relation_file($files,'archive_zhuan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'archive_zhuan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'archive_zhuan', $id, '转正');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('archive_zhuan');
            $data['isadvan'] = (new FlowService())->check_isadvan('archive_zhuan');
            $data['isnext'] = (new FlowService())->check_isnext('archive_zhuan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_archive_zhuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['intodt'])){
                    return outputJson(4000, '请选择入职日期');
                }
                if(empty($param['shidaodt'])){
                    return outputJson(4000, '请选择试用期到期日期');
                }
                if($param['gongfee'] == ''){
                    return outputJson(4000, '请输入试用期工资');
                }
                if(empty($param['zhuandt'])){
                    return outputJson(4000, '请选择申请转正日期');
                }
                if($param['zhuanfee'] == ''){
                    return outputJson(4000, '请输入转正工资');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ArchiveZhuanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('archive_zhuan', $param['id']);
                }else{
                    (new FlowService())->save_bill('archive_zhuan', $param['id']);
                }

                (new OptService())->relation_file($files,'archive_zhuan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'archive_zhuan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'archive_zhuan', $param['id'], '转正');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ArchiveZhuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['files'] = (new OptService())->get_file('archive_zhuan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('archive_zhuan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('archive_zhuan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('archive_zhuan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_archive_out(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['intodt'])){
                    return outputJson(4000, '请选择入职日期');
                }
                if(empty($param['outdt'])){
                    return outputJson(4000, '请选择申请离职日期');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ArchiveOutModel::insertGetId($param);

                (new FlowService())->save_bill('archive_out', $id);

                (new OptService())->relation_file($files,'archive_out',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'archive_out', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'archive_out', $id, '离职');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('archive_out');
            $data['isadvan'] = (new FlowService())->check_isadvan('archive_out');
            $data['isnext'] = (new FlowService())->check_isnext('archive_out');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_archive_out(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['intodt'])){
                    return outputJson(4000, '请选择入职日期');
                }
                if(empty($param['outdt'])){
                    return outputJson(4000, '请选择申请离职日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                ArchiveOutModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('archive_out', $param['id']);
                }else{
                    (new FlowService())->save_bill('archive_out', $param['id']);
                }

                (new OptService())->relation_file($files,'archive_out',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'archive_out', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'archive_out', $param['id'], '离职');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ArchiveOutModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['files'] = (new OptService())->get_file('archive_out', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('archive_out');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('archive_out',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('archive_out');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_perru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入人员姓名');
                }
                if((new CertService())->check_cert_person_name($param['name'])){
                    return '人员姓名已存在';
                }
                if(!empty($param['idcard'])){
                    if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                        return '人员身份证号码错误';
                    }
                    if((new CertService())->check_person_idcard_exist($param['idcard'])){
                        return '人员身份证号码已存在';
                    }
                }

                if(empty($param['deptid'])){
                    return outputJson(4000, '请选择所在部门');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = CertPerruModel::insertGetId($param);

                (new FlowService())->save_bill('cert_perru', $id);

                (new OptService())->relation_file($files,'cert_perru',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_perru', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_perru', $id, '入职审批');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['deptArr'] = (new UserService())->get_deptinfo_all();
            $data['xueli'] = (new OptService())->get_base_info('person_xueli');
            $data['nature'] = (new OptService())->get_base_info('person_nature');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['gangArr'] = (new UserService())->get_usergang_all();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_perru');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_perru');
            $data['isnext'] = (new FlowService())->check_isnext('cert_perru');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_perru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入人员姓名');
                }
                if((new CertService())->check_cert_person_name($param['name'])){
                    return '人员姓名已存在';
                }
                if(!empty($param['idcard'])){
                    if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                        return '人员身份证号码错误';
                    }
                    if((new CertService())->check_person_idcard_exist($param['idcard'])){
                        return '人员身份证号码已存在';
                    }
                }

                if(empty($param['deptid'])){
                    return outputJson(4000, '请选择所在部门');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                CertPerruModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_perru', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_perru', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_perru',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_perru', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_perru', $param['id'], '入职审批');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertPerruModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['gangid'] = explode(',', $data['data']['gangid']);
            $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['deptid'], 'dept');

            $data['deptArr'] = (new UserService())->get_deptinfo_all();
            $data['xueli'] = (new OptService())->get_base_info('person_xueli');
            $data['nature'] = (new OptService())->get_base_info('person_nature');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['gangArr'] = (new UserService())->get_usergang_all();

            $data['files'] = (new OptService())->get_file('cert_perru', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_perru');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_perru',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_perru');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_zhao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['gangxu'])){
                    return outputJson(4000, '请输入需求岗位');
                }
                if(empty($param['zhaonum'])){
                    return outputJson(4000, '请输入招聘人数');
                }
                if(empty($param['xincon'])){
                    return outputJson(4000, '请输入薪酬建议');
                }
                if(empty($param['zhaocon'])){
                    return outputJson(4000, '请输入招聘原因');
                }
                if(empty($param['gangcon'])){
                    return outputJson(4000, '请输入岗位职责');
                }
                if(empty($param['rencon'])){
                    return outputJson(4000, '请输入任职条件');
                }
                if(empty($param['qidt'])){
                    return outputJson(4000, '请选择期望到岗日期');
                }
                if(empty($param['jindu'])){
                    return outputJson(4000, '请选择紧急程度');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = CertZhaoModel::insertGetId($param);

                (new FlowService())->save_bill('cert_zhao', $id);

                (new OptService())->relation_file($files,'cert_zhao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_zhao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_zhao', $id, '招聘需求');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['deptxu_info'] = (new UserService())->get_users_part_arr($this->userinfo['deptid'], 'dept');
            $data['jindu'] = (new CertService())->get_cert_zhao_jindu();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_zhao');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_zhao');
            $data['isnext'] = (new FlowService())->check_isnext('cert_zhao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_zhao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['gangxu'])){
                    return outputJson(4000, '请输入需求岗位');
                }
                if(empty($param['zhaonum'])){
                    return outputJson(4000, '请输入招聘人数');
                }
                if(empty($param['xincon'])){
                    return outputJson(4000, '请输入薪酬建议');
                }
                if(empty($param['zhaocon'])){
                    return outputJson(4000, '请输入招聘原因');
                }
                if(empty($param['gangcon'])){
                    return outputJson(4000, '请输入岗位职责');
                }
                if(empty($param['rencon'])){
                    return outputJson(4000, '请输入任职条件');
                }
                if(empty($param['qidt'])){
                    return outputJson(4000, '请选择期望到岗日期');
                }
                if(empty($param['jindu'])){
                    return outputJson(4000, '请选择紧急程度');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                CertZhaoModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_zhao', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_zhao', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_zhao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_zhao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_zhao', $param['id'], '招聘需求');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertZhaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['deptxu_info'] = (new UserService())->get_users_part_arr($data['data']['deptxu'], 'dept');

            $data['jindu'] = (new CertService())->get_cert_zhao_jindu();

            $data['files'] = (new OptService())->get_file('cert_zhao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_zhao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_zhao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_zhao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['guidt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
                if(empty($param['purpose'])){
                    return outputJson(4000, '请输入用途');
                }
            }


            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();
            $newparam['flowuser'] = $this->userinfo['realname'];
            $newparam['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅档案明细');
                }
            }


            $newparam['isturn'] = $param['isturn'];
            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];
            $newparam['guidt'] = $param['guidt'];
            $newparam['purpose'] = $param['purpose'];
            $newparam['explain'] = $param['explain'];

            $kknum = 1;
            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];

                if($param['isturn'] > 0){
                    if($vv['num'] > $vv['kejie']){
                        return outputJson(4000, '第'.$kknum.'行借阅数量已超档案可借数量');
                    }
                }
                $kknum++;
            }




            Db::startTrans();
            try {
                $id = DangJieModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DangJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('dang_jie', $id);

                (new OptService())->relation_file($files,'dang_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dang_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dang_jie', $id, '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['useer'] = $this->userinfo['realname'];
            $data['depter'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('dang_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('dang_jie');
            $data['isnext'] = (new FlowService())->check_isnext('dang_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['guidt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
                if(empty($param['purpose'])){
                    return outputJson(4000, '请输入用途');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅档案明细');
                }
            }



            $newparam['id'] = $param['id'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];
            $newparam['guidt'] = $param['guidt'];
            $newparam['purpose'] = $param['purpose'];
            $newparam['explain'] = $param['explain'];

            $kknum = 1;
            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];

                if($param['isturn'] > 0){
                    if($vv['num'] > $vv['kejie']){
                        return outputJson(4000, '第'.$kknum.'行借阅数量已超档案可借数量');
                    }
                }
                $kknum++;
            }

            Db::startTrans();
            try {
                DangJieModel::update($newparam);

                DangJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DangJieListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dang_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('dang_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'dang_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dang_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dang_jie', $param['id'], '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DangJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $list = DangJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $dang = DangModel::where(['id'=>$item['dangid']])->find();
                $item['name'] = $dang['name'];
                $item['number'] = $dang['number'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('dang_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dang_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dang_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dang_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kao_jia(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KaoJiaModel::insertGetId($param);

                (new FlowService())->save_bill('kao_jia', $id);

                (new OptService())->relation_file($files,'kao_jia',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kao_jia', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kao_jia', $id, '请假');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jiatype'] = (new OptService())->get_base_info('qi_kao_jiatype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('kao_jia');
            $data['isadvan'] = (new FlowService())->check_isadvan('kao_jia');
            $data['isnext'] = (new FlowService())->check_isnext('kao_jia');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kao_jia(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                KaoJiaModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kao_jia', $param['id']);
                }else{
                    (new FlowService())->save_bill('kao_jia', $param['id']);
                }

                (new OptService())->relation_file($files,'kao_jia',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kao_jia', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kao_jia', $param['id'], '请假');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaoJiaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jiatype'] = (new OptService())->get_base_info('qi_kao_jiatype');
            $data['files'] = (new OptService())->get_file('kao_jia', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kao_jia');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kao_jia',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kao_jia');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kao_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dest'])){
                    return outputJson(4000, '请输入目的地');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KaoChuModel::insertGetId($param);

                (new FlowService())->save_bill('kao_chu', $id);

                (new OptService())->relation_file($files,'kao_chu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kao_chu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kao_chu', $id, '外出');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('kao_chu');
            $data['isadvan'] = (new FlowService())->check_isadvan('kao_chu');
            $data['isnext'] = (new FlowService())->check_isnext('kao_chu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kao_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dest'])){
                    return outputJson(4000, '请输入目的地');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                KaoChuModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kao_chu', $param['id']);
                }else{
                    (new FlowService())->save_bill('kao_chu', $param['id']);
                }

                (new OptService())->relation_file($files,'kao_chu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kao_chu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kao_chu', $param['id'], '外出');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaoChuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['files'] = (new OptService())->get_file('kao_chu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kao_chu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kao_chu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kao_chu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kao_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KaoBanModel::insertGetId($param);

                (new FlowService())->save_bill('kao_ban', $id);

                (new OptService())->relation_file($files,'kao_ban',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kao_ban', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kao_ban', $id, '加班');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('kao_ban');
            $data['isadvan'] = (new FlowService())->check_isadvan('kao_ban');
            $data['isnext'] = (new FlowService())->check_isnext('kao_ban');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kao_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                KaoBanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kao_ban', $param['id']);
                }else{
                    (new FlowService())->save_bill('kao_ban', $param['id']);
                }

                (new OptService())->relation_file($files,'kao_ban',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kao_ban', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kao_ban', $param['id'], '加班');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaoBanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['files'] = (new OptService())->get_file('kao_ban', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kao_ban');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kao_ban',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kao_ban');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kao_chai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chudi'])){
                    return outputJson(4000, '请输入出发地');
                }
                if(empty($param['dest'])){
                    return outputJson(4000, '请输入目的地');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KaochaiModel::insertGetId($param);

                (new FlowService())->save_bill('kao_chai', $id);

                (new OptService())->relation_file($files,'kao_chai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kao_chai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kao_chai', $id, '出差');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('kao_chai');
            $data['isadvan'] = (new FlowService())->check_isadvan('kao_chai');
            $data['isnext'] = (new FlowService())->check_isnext('kao_chai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kao_chai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chudi'])){
                    return outputJson(4000, '请输入出发地');
                }
                if(empty($param['dest'])){
                    return outputJson(4000, '请输入目的地');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择结束时间');
                }
                if(empty($param['duration'])){
                    return outputJson(4000, '请输入时长');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                KaochaiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kao_chai', $param['id']);
                }else{
                    (new FlowService())->save_bill('kao_chai', $param['id']);
                }

                (new OptService())->relation_file($files,'kao_chai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kao_chai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kao_chai', $param['id'], '出差');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaochaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['files'] = (new OptService())->get_file('kao_chai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kao_chai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kao_chai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kao_chai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();



            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();
            $newparam['flowuser'] = $this->userinfo['realname'];
            $newparam['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['isturn'] = $param['isturn'];
            $newparam['advaners'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';
            $newparam['con'] = $param['con'];


            Db::startTrans();
            try {
                $id = SealGaiModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['gaiid'] = $id;
                    SealGaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('seal_gai', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_gai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_gai', $id, '日常盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();


            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_gai');
            $data['isnext'] = (new FlowService())->check_isnext('seal_gai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $newparam['created_at'] = date('Y-m-d H:i:s');
            }

            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['id'] = $param['id'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';
            $newparam['con'] = $param['con'];
            Db::startTrans();
            try {
                SealGaiModel::update($newparam);

                SealGaiListModel::where(['gaiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gaiid'] = $param['id'];
                    SealGaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_gai', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_gai', $param['id']);
                }
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'seal_gai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'seal_gai', $param['id'], '日常盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealGaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();

            $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_gai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_gai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择计划发放日期');
                }
            }



            $param['number'] = (new HumanService())->create_wage_number($param['month']);
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                $id = WageModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['wageid'] = $id;
                    WageListModel::insert($vv);
                }

                (new FlowService())->save_bill('wage', $id);

                (new OptService())->relation_file($files,'wage',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wage', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wage', $id, '工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wage');
            $data['isadvan'] = (new FlowService())->check_isadvan('wage');
            $data['isnext'] = (new FlowService())->check_isnext('wage');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择计划发放日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                WageModel::update($param);

                WageListModel::where(['wageid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['wageid'] = $param['id'];
                    WageListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wage', $param['id']);
                }else{
                    (new FlowService())->save_bill('wage', $param['id']);
                }

                (new OptService())->relation_file($files,'wage',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wage', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wage', $param['id'], '工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WageModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WageListModel::where(['wageid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wage', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wage');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wage',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wage');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wen_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['wentype'])){
                    return outputJson(4000, '请选择公文类型');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择拟稿日期');
                }
                if(empty($param['wenzi'])){
                    return outputJson(4000, '请选择发文字号');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入公文编号');
                }
                if(empty($param['wenlevel'])){
                    return outputJson(4000, '请选择公文秘级');
                }
                if(empty($param['wenjin'])){
                    return outputJson(4000, '请选择紧急程度');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['fadept'])){
                    return outputJson(4000, '请输入发文单位');
                }
                if(empty($param['zhudept'])){
                    return outputJson(4000, '请输入主送单位');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = WenBanModel::insertGetId($param);

                (new FlowService())->save_bill('wen_ban', $id);

                (new OptService())->relation_file($files,'wen_ban',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wen_ban', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wen_ban', $id, '拟办发文');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['year'] = date("Y");
            $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
            $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wen_ban');
            $data['isadvan'] = (new FlowService())->check_isadvan('wen_ban');
            $data['isnext'] = (new FlowService())->check_isnext('wen_ban');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wen_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['wentype'])){
                    return outputJson(4000, '请选择公文类型');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择拟稿日期');
                }
                if(empty($param['wenzi'])){
                    return outputJson(4000, '请选择发文字号');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入公文编号');
                }
                if(empty($param['wenlevel'])){
                    return outputJson(4000, '请选择公文秘级');
                }
                if(empty($param['wenjin'])){
                    return outputJson(4000, '请选择紧急程度');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['fadept'])){
                    return outputJson(4000, '请输入发文单位');
                }
                if(empty($param['zhudept'])){
                    return outputJson(4000, '请输入主送单位');
                }
            }



            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                WenBanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wen_ban', $param['id']);
                }else{
                    (new FlowService())->save_bill('wen_ban', $param['id']);
                }

                (new OptService())->relation_file($files,'wen_ban',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wen_ban', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wen_ban', $param['id'], '拟办发文');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WenBanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['deptid'], 'dept');
            $data['data']['zhudept_info'] = (new UserService())->get_users_part_arr($data['data']['zhudept'], 'dept');
            $data['data']['chaodept_info'] = (new UserService())->get_users_part_arr($data['data']['chaodept'], 'dept');


            $data['year'] = date("Y");
            $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
            $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            $data['files'] = (new OptService())->get_file('wen_ban', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wen_ban');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wen_ban',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wen_ban');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入供应方');
                }
            }


            $param['number'] = (new XingService())->create_wu_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购物品明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    WuCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_cai', $id);

                (new OptService())->relation_file($files,'wu_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_cai', $id, '物品采购');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new XingService())->create_wu_cai_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_cai');
            $data['isnext'] = (new FlowService())->check_isnext('wu_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入供应方');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购物品明细');
                }
            }


            Db::startTrans();
            try {
                WuCaiModel::update($param);

                WuCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    WuCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_cai', $param['id'], '物品采购');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
            }

            $param['number'] = (new XingService())->create_wu_ling_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    WuLingListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_ling', $id);

                (new OptService())->relation_file($files,'wu_ling',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ling', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ling', $id, '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new XingService())->create_wu_ling_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_ling');
            $data['isnext'] = (new FlowService())->check_isnext('wu_ling');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }


            Db::startTrans();
            try {
                WuLingModel::update($param);

                WuLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    WuLingListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_ling', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_ling', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_ling',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ling', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ling', $param['id'], '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['runum'] = WuRuListModel::alias('list')
                    ->join('wu_ru ru','ru.id=list.ruid')
                    ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->sum('list.num');
                $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
                $item['chunum'] += WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where('list.id','neq',$item['id'])
                    ->sum('list.num');
                $item['kucun'] = $item['runum']-$item['chunum'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_ling', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_ling',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_ling');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TongyongModel::insertGetId($param);
                (new FlowService())->save_bill('tongyong', $id);
                (new OptService())->relation_file($files,'tongyong',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $id, '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['isadvan'] = (new FlowService())->check_isadvan('tongyong');
            $data['isnext'] = (new FlowService())->check_isnext('tongyong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                TongyongModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tongyong', $param['id']);
                }else{
                    (new FlowService())->save_bill('tongyong', $param['id']);
                }

                (new OptService())->relation_file($files,'tongyong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $param['id'], '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TongyongModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');

            $data['files'] = (new OptService())->get_file('tongyong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tongyong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tongyong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
















}
